Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Dictionary Return Type

I've got a problem with some c# code I'm writing, I'm fairly new to c# and I've had a look around and can't find a solution.

I've got a method that returns a Dictionary, I've set the return type to object and it seems ok.

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


        ... return countQuery;
    }

The problem is in the main method where I'm trying to loop through the elements returned from the dictionary.

                Notification notification = new Notification();

                var countDictionary = notification.loopThroughNotificationCountQueries();


                foreach(KeyValuePair<String, String> entry in countDictionary)
                {
                    ...
                }

I'm getting an error saying "Error 2 foreach statement cannot operate on variables of type 'object' because 'object' does not contain a public definition for 'GetEnumerator'"

Is it because I'm not specifying the correct return type for a dictionary? Or is there another way of iterating through the entries in the returned object?

Thanks for your help, Stephen.

like image 660
user1035479 Avatar asked Nov 08 '11 11:11

user1035479


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


3 Answers

Look at your method declaration:

public object loopThroughNotificationCountQueries()

That means your countDictionary declaration is effectively:

object countDictionary = notification.loopThroughNotificationCountQueries();

... and you can't use foreach with an object like that. The simplest fix is to change the method declaration, e.g. to

// Note case change as well to follow .NET naming conventions
public IDictionary<string, string> LoopThroughNotificationCountQueries()
like image 171
Jon Skeet Avatar answered Sep 19 '22 21:09

Jon Skeet


Use

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

or explain why that's not possible.

like image 35
Henk Holterman Avatar answered Sep 22 '22 21:09

Henk Holterman


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


        ... return countQuery;
    }
like image 27
alexl Avatar answered Sep 19 '22 21:09

alexl