【问题标题】:Using GetEnumerator for Dictionary initialization C#使用 GetEnumerator 进行字典初始化 C#
【发布时间】:2016-05-27 15:25:55
【问题描述】:

我有一个类Cluster.cs 定义为:

public class Cluster
{
        protected int _clusterID = -1;
        private static int _clusterCount = 0;

        protected int _attributeCount;
        // _clusterObjects contains EvoObjects in this cluster. 
        protected List<EvoObject> _clusterObjects = new List<EvoObject>();

        /** _nkA[_i] is the total occurrences of the attribute _i in this cluster*/
        protected Int32[] _nkA;

        // For each attribute, record their values as KeyValuePair.

        protected Dictionary<Int32, UtilCS.KeyCountMap<Int32>> _attributeValues = new Dictionary<Int32, UtilCS.KeyCountMap<Int32>>();

        public Cluster(int _clusterID, int _attributeCount)
        {
            this._clusterID = _clusterID;
            this._attributeCount = _attributeCount;
            _nkA = new Int32[_attributeCount];
        }

        // Initialize _attributeValues
        IEnumerable<Int32>  _range = Enumerable.Range(0, _attributeCount).GetEnumerator(_i => {_attributeValues[_i] = new UtilCS.KeyCountMap<Int32>()});
}  

在初始化_attributeValues 时,我收到错误:

“GetEnumerator 的重载方法不接受 1 个参数”

而我只有 1 个参数来初始化,即 _attributeValues 这实际上是一本字典,这就是为什么必须枚举它的原因。

另外,如果我声明 _attributeCount 静态,我无法在构造函数中使用它,如果我声明它为非静态,我将无法使用它 Range 的 Enumberable 方法。

那么我将如何初始化_attributeValues
如何声明_attributeCount 静态或非静态?

【问题讨论】:

    标签: c# dictionary static initialization enumeration


    【解决方案1】:

    看起来您试图通过使用在对象创建时设置的变量 (_range) 来初始化 Dictionary。这应该移到构造函数中。您似乎正在尝试使用键 0、1、2 和 new UtilCS.KeyCountMap() 的值创建三个新属性。

    如果这是正确的,那么我建议使用它作为你的构造函数。

    public Cluster(int _clusterID, int _attributeCount)
    {
      this._clusterID = _clusterID;
      this._attributeCount = _attributeCount;
      _nkA = new Int32[_attributeCount];
    
      for (var i = 0; i < 3; i++)
      {
        _attributeValues.Add(i, new UtilCS.KeyCountMap<int>());
      }
    }
    

    但是,由于您发送的是 _attributeCount,我想说您可以使用它而不是 3。

    public Cluster(int _clusterID, int _attributeCount)
    {
      this._clusterID = _clusterID;
      this._attributeCount = _attributeCount;
      _nkA = new Int32[_attributeCount];
    
      for (var i = 0; i < _attributeCount; i++)
      {
        _attributeValues.Add(i, new UtilCS.KeyCountMap<int>());
      }
    }
    

    【讨论】:

    • 那么我们不需要对字典对象进行任何枚举来初始化吗?我希望for() 循环能解决问题?
    • 如果您仍然想要看起来是键枚举的 _range ?您可以使用诸如 public IEnumerable _range { get{return _attributeValues.Keys; 之类的属性。 } }
    • 字典被初始化为一个空字典已经在这一行:protected Dictionary> _attributeValues = new Dictionary>();
    • 但是如果我们在构造函数中使用 for 循环来初始化它,它会创建新的属性和值(无论_attributeCount 的值是什么)对吗?
    • 您似乎在考虑数组的思路,您需要将其初始化为特定大小,然后您可以稍后添加值,直到该大小但不能超过。字典不是那样的,你将它初始化为一个新的字典,然后你可以将 KeyValuePairs 添加到那个字典中。看来您正在为每个值使用顺序键,您可以使用 List 来实现这一点并跳过字典。这将允许您向每个列表添加任意数量的对象。
    【解决方案2】:
    Enumerable.Range(0, _attributeCount).ToList().ForEach(x => _attributeValues.Add(x, new UtilCS.KeyCountMap<Int32>()));
    

    【讨论】:

    • @Ponas——这是 C#。我们这里有 ForEach() 方法吗?
    • @Taufel 是的,看看here
    • “字段初始值设定项无法引用非静态字段”_attributeCountRange(0, _attributeCount) 方法中引发的错误
    猜你喜欢
    • 1970-01-01
    • 2021-11-28
    • 2011-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    相关资源
    最近更新 更多