【问题标题】:C# Dictionary Return TypeC# 字典返回类型
【发布时间】:2011-12-24 09:05:16
【问题描述】:

我正在编写的一些 c# 代码有问题,我对 c# 还很陌生,我环顾四周,找不到解决方案。

我有一个返回字典的方法,我已经将返回类型设置为对象,看起来没问题。

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


        ... return countQuery;
    }

问题出在我试图遍历从字典返回的元素的主要方法中。

                Notification notification = new Notification();

                var countDictionary = notification.loopThroughNotificationCountQueries();


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

我收到一条错误消息,提示“错误 2 foreach 语句无法对 'object' 类型的变量进行操作,因为 'object' 不包含 'GetEnumerator' 的公共定义”

是因为我没有为字典指定正确的返回类型吗?还是有另一种方法可以遍历返回对象中的条目?

感谢您的帮助, 斯蒂芬。

【问题讨论】:

    标签: c# types dictionary return


    【解决方案1】:

    你不能有如下的方法签名有什么原因吗?你总是返回一个带有字符串键类型和字符串数据类型的字典吗?

    public Dictionary<string, string> loopThroughNotificationCountQueries() 
    

    【讨论】:

      【解决方案2】:
      public IDictionary<string, string> loopThroughNotificationCountQueries()
          {
              var countQuery = new Dictionary<string, string>(); ...
      
      
              ... return countQuery;
          }
      

      【讨论】:

        【解决方案3】:

        看看你的方法声明:

        public object loopThroughNotificationCountQueries()
        

        这意味着您的countDictionary 声明有效:

        object countDictionary = notification.loopThroughNotificationCountQueries();
        

        ...你不能像这样使用foreachobject。最简单的解决方法是更改​​方法声明,例如到

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

        【讨论】:

        • 感谢您的帮助,正如我所说我是 C# 新手,我不知道方法可以声明为字典。
        【解决方案4】:

        使用

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

        或解释为什么这是不可能的。

        【讨论】:

        • @Vlad - 什么访问修饰符?你的意思是返回类型吗?
        • @Vlad - 我知道访问修饰符是什么。我只是不明白为什么您认为它们与答案相关。
        • public IDictionary 会更好。 (而不是字典)
        • @Oded 诚然,我不是很喜欢stackoverflowy,但我发现Henk 的错字很有趣。答案也是有效的,所以我支持我的 +1。
        【解决方案5】:

        是的,因为您没有指定返回类型。

        两种可能:

        更好:你指定返回类型到 Dictionary

        更糟:你在调用方法中将对象转换为字典

        【讨论】:

        • 他确实指定了返回类型 - object
        • 哈哈。就像我在一本书的摘要中写的:“它是一本书”是的,它是正确的
        • 如果这里不指定返回类型,他会使用void
        • void 意味着方法声明没有返回类型。但是这个有一个。它只是不正确指定。 ;-)
        【解决方案6】:

        您的loopThroughNotificationCountQueries 返回object。通过更改其签名使其返回Dictionary&lt;string, string&gt;

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

        【讨论】:

          【解决方案7】:

          是的,应该是:

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

          你只能遍历IEnumerable&lt;T&gt;的对象

          因此,如果由于某种原因您无法更改 loopThroughNotificationCountQueries,请先将对象转换为 IDictionary&lt;string, string&gt;

          【讨论】:

            【解决方案8】:

            你不应该在非私有方法中返回字典,它会暴露类型,以及它的所有方法和属性,如果你不需要它们,而且在大多数情况下你不应该,不要。 打开 FxCop,它会因为你这样做而对你嚎叫

            有很多方法可以解决这个问题,您想做 SomeClass.SomeDictionary.Add("name","value") 的机会很小,作为一个明智的实现的机会几乎不存在。

            一般来说,我只是让我的班级有一个 Dictionary Type 的私有成员并公开一些方法 例如

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

            等等

            如果我经常做这件事,请委托给一个简单的课程并随身携带。

            【讨论】:

              猜你喜欢
              • 2019-02-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2018-12-24
              • 1970-01-01
              • 1970-01-01
              • 2020-05-22
              • 2012-10-18
              相关资源
              最近更新 更多