小编use*_*479的帖子

C#字典返回类型

我在写一些c#代码时遇到了问题,我对c#很新,而且我已经浏览了一下,无法找到解决方案.

我有一个返回Dictionary的方法,我已经将返回类型设置为object,看起来没问题.

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


        ... return countQuery;
    }
Run Code Online (Sandbox Code Playgroud)

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

                Notification notification = new Notification();

                var countDictionary = notification.loopThroughNotificationCountQueries();


                foreach(KeyValuePair<String, String> entry in countDictionary)
                {
                    ...
                }
Run Code Online (Sandbox Code Playgroud)

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

是因为我没有为字典指定正确的返回类型吗?或者是否有另一种方法来迭代返回对象中的条目?

谢谢你的帮助,斯蒂芬.

c# dictionary types return

5
推荐指数
2
解决办法
4万
查看次数

标签 统计

c# ×1

dictionary ×1

return ×1

types ×1