简体   繁体   English

C#字典返回类型

[英]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. 我在写一些c#代码时遇到了问题,我对c#很新,而且我已经浏览了一下,无法找到解决方案。

I've got a method that returns a Dictionary, I've set the return type to object and it seems ok. 我有一个返回Dictionary的方法,我已经将返回类型设置为object,看起来没问题。

    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'" 我收到一条错误,说“错误2 foreach语句无法对'object'类型的变量进行操作,因为'object'不包含'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. 谢谢你的帮助,斯蒂芬。

Look at your method declaration: 看看你的方法声明:

public object loopThroughNotificationCountQueries()

That means your countDictionary declaration is effectively: 这意味着你的countDictionary声明是有效的:

object countDictionary = notification.loopThroughNotificationCountQueries();

... and you can't use foreach with an object like that. ...而且你不能将foreach与这样的object一起使用。 The simplest fix is to change the method declaration, eg to 最简单的解决方法是更改​​方法声明,例如

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

Use 使用

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

or explain why that's not possible. 或者解释为什么那是不可能的。

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


        ... return countQuery;
    }

Is there a reason you can't have your method signature as below? 您是否有理由不能使用下面的方法签名? Do you always return a dictionary with a string key type and a string data type? 您是否总是返回字符串键类型和字符串数据类型的字典?

public Dictionary<string, string> loopThroughNotificationCountQueries() 

your loopThroughNotificationCountQueries returns object . 你的loopThroughNotificationCountQueries返回object Make it return Dictionary<string, string> by changing its signature. 通过更改其签名使其返回Dictionary<string, string>

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


    ... return countQuery;
}

yes, it's suppose to be: 是的,它假设是:

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

You can only itterate through objects of IEnumerable<T> 您只能通过IEnumerable<T>对象进行迭代

so if for some reason you cannot change loopThroughNotificationCountQueries , cast the object to an IDictionary<string, string> first. 因此,如果由于某种原因您无法更改loopThroughNotificationCountQueriesloopThroughNotificationCountQueries将对象loopThroughNotificationCountQueries转换为IDictionary<string, string>

yes its because you dont specify the return type. 是的,因为你没有指定返回类型。

two possibilities: 两种可能性:

the better: you specify the return type to Dictionary 更好:您将返回类型指定为Dictionary

the worse: you cast the object to a dictionary in the calling method 更糟糕的是:您将对象转换为调用方法中的字典

You should return not a dictionary in non-private method, it's exposing the type, and all it's methods and properties, if you don't need them ,and you shouldn't in most cases, don't. 你应该在非私有方法中返回不是字典,它暴露了类型,以及它的所有方法和属性,如果你不需要它们,你不应该在大多数情况下不这样做。 Turn on FxCop and it will howl at you for doing this 打开FxCop,它会嚎叫你这样做

Lots of ways round it, the chances of you wanting to do SomeClass.SomeDictionary.Add("name","value") are small, the chance of that being a sensible implementation are near non-existent. 围绕它的方式很多,你想要做SomeClass.SomeDictionary.Add(“名称”,“值”)的机会很小,这是一个明智的实现的可能性几乎不存在。

In general I simply have my class have a private member of Dictionary Type and expose a few methods eg 一般来说,我只是让我的类有一个Dictionary Type的私有成员,并公开一些方法,例如

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

etc. 等等

If I'm doing it a lot, delegate to a simple class and carry that about. 如果我正在做很多事情,请委托一个简单的课程来实现它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM