简体   繁体   English

使用GetEnumerator进行字典初始化C#

[英]Using GetEnumerator for Dictionary initialization C#

I have a class Cluster.cs defined as: 我有一个类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>()});
}  

while initializing _attributeValues , I got error as: 在初始化_attributeValues ,我收到错误:

"No overloaded method for GetEnumerator takes 1 argument" “GetEnumerator没有重载方法需要1个参数”

whereas I've only 1 argument to initialize ie _attributeValues which is actually a dictionary that's why have to enumerate at it. 我只有一个参数来初始化ie _attributeValues ,这实际上是一个字典,这就是为什么必须枚举它。

Also if I declare _attributeCount static, I'm unable to use it in constructor and if I declar it non-static, I'm unable to use it Range method of Enumberable. 此外,如果我声明_attributeCount静态,我无法在构造函数中使用它,如果我声明它是非静态的,我无法使用它的Enumberable的Range方法。

How will I initialize _attributeValues then? 我如何初始化_attributeValues呢?
How to declare _attributeCount static or non-static? 如何声明_attributeCount静态还是非静态?

Enumerable.Range(0, _attributeCount).ToList().ForEach(x => _attributeValues.Add(x, new UtilCS.KeyCountMap<Int32>()));

It looks like your trying to initialize the Dictionary by using a variable (_range) that is set at object creation. 看起来你试图通过使用在创建对象时设置的变量(_range)来初始化Dictionary。 This should be moved into the constructor. 这应该移动到构造函数中。 It appears you are attempting to create three new attributes with keys 0,1,2 and values of new UtilCS.KeyCountMap(). 您似乎正在尝试使用键0,1,2和新UtilCS.KeyCountMap()的值创建三个新属性。

If this is correct, then I would suggest using this as your constructor. 如果这是正确的,那么我建议使用它作为你的构造函数。

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>());
  }
}

However, since you are sending in _attributeCount I would say you could use that instead of 3. 但是,由于您要发送_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>());
  }
}

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

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