C # dictionary return type

I have a problem with the C # code I'm writing, I'm pretty new to C #, and I looked around and can't find a solution.

I have a method that returns a dictionary, I set the return type to the object, and it looks fine.

public object loopThroughNotificationCountQueries() { var countQuery = new Dictionary<string, string>(); ... ... return countQuery; } 

The problem lies in the main method in which I try to iterate over elements returned from a dictionary.

  Notification notification = new Notification(); var countDictionary = notification.loopThroughNotificationCountQueries(); foreach(KeyValuePair<String, String> entry in countDictionary) { ... } 

I get the error message "Error 2 foreach statement cannot work with variables of type" object "because" object "does not contain a public definition for" GetEnumerator "

Is it because I am not specifying the correct return type for the dictionary? Or is there another way to repeat the entries in the returned object?

Thanks for all your help, Stephen.

+4
source share
8 answers

Look at the declaration of your method:

 public object loopThroughNotificationCountQueries() 

This means that your countDictionary declaration countDictionary effective:

 object countDictionary = notification.loopThroughNotificationCountQueries(); 

... and you cannot use foreach with such an object . The simplest fix is ​​to change the method declaration, for example. to

 // Note case change as well to follow .NET naming conventions public IDictionary<string, string> LoopThroughNotificationCountQueries() 
+10
source

Using

 public Dictionary<string, string> loopThroughNotificationCountQueries() { ... } 

or explain why this is not possible.

+7
source
 public IDictionary<string, string> loopThroughNotificationCountQueries() { var countQuery = new Dictionary<string, string>(); ... ... return countQuery; } 
+4
source

Is there a reason why you cannot have your method signature as shown below? Do you always return a dictionary with a string key and a string data type?

 public Dictionary<string, string> loopThroughNotificationCountQueries() 
+1
source

your loopThroughNotificationCountQueries returns an object . Make it return Dictionary<string, string> , changing its signature.

 public Dictionary<string, string> loopThroughNotificationCountQueries() { var countQuery = new Dictionary<string, string>(); ... ... return countQuery; } 
+1
source

yes it should be:

 public IDictionary<string, string> loopThroughNotificationCountQueries() { } 

You can only iterate over IEnumerable<T> objects

therefore, if for some reason you cannot change loopThroughNotificationCountQueries , first transfer the object to IDictionary<string, string> .

+1
source

yes, because you do not specify the type of return.

two possibilities:

better: you specify the type of return to the dictionary

worse: you pass an object to a dictionary in the calling method

0
source

You must not return a dictionary in a non-private method, it exposes the type and all its methods and properties if you do not need them, and you should not do this in most cases. Turn on FxCop and it will howl at you for it

There are many ways to get around it, the chances that you want to make SomeClass.SomeDictionary.Add ("name", "value") are small, the probability that this is a reasonable implementation almost does not exist.

In general, I just have a class that has a private member like a dictionary and exposes several methods for example.

 public IEnumerable<String> Names { get { return _myDictionary.Keys;} } 

and etc.

If I do this a lot, pass a simple class and carry it.

0
source

Source: https://habr.com/ru/post/1380161/


All Articles