English 中文(简体)
习俗何时可以计算/收集?
原标题:When is custom enumerable/collection useful?
  • 时间:2009-10-21 10:58:24
  •  标签:

我在访问了不同的网站,以了解使用习俗统计的实际时间例子后,就放弃了这一界限。 我举几个例子。 但是,它们给我造成了混乱。

<><>Example>

<><0>

class NumberArray
{
    public int[] scores;

    public NumberArray()
    {
    }

    public NumberArray(int[] scores)
    {
        this.scores = scores;
    }

    public int[] Scores
    {
        get {return scores;}
    }

}

www.un.org/Depts/DGACM/index_spanish.htm 第2条:

public class Enumerator : IEnumerator
{
    int[] scores;
    int cur;
    public Enumerator(int[] scores)
    {
        this.scores = scores;
        cur = -1;
    }
    public Object Current
    {
        get {
            return scores[cur];
        }
    }

    public void Reset()
    {
        cur = -1;
    }

    public bool MoveNext()
    {
        cur++;
        if (cur < scores.Length)
            return true;
        else return false;
    }
}

public class Enumerable : IEnumerable
{
    int[] numbers;

    public void GetNumbersForEnumeration(int[] values)
    {
        numbers = values;
        for (int i = 0; i < values.Length; i++)
            numbers[i] = values[i];
    }

    public IEnumerator GetEnumerator()
    {
        return new Enumerator(numbers);
    }
}

http://www.ohchr.org。

static void Main()
{
    int[] arr = new int[] { 1, 2, 3, 4, 5 };
    NumberArray num = new NumberArray(arr);
    foreach(int val in num.Scores)
    {
        Console.WriteLine(val);
    }

    Enumerable en = new Enumerable();
    en.GetNumbersForEnumeration(arr);

    foreach (int i in en)
    {
        Console.WriteLine(i);
    }
    Console.ReadKey(true);
}

In take 2, I followed the custom iteration to iterate the same integer array as I did in take 1. Why should I beat about the bush to iterate an integer by using custom iteration?

或许,我忽略了实时定制中程>需要。 您能否解释一下我可以与现有回收设施做些什么? (I 刚刚结束学业,因此给我一个简单的例子,以便我能够适当理解。

Update : I took those examples from some site. There is nothing special in that code, we can achieve it very simply even without using custom iteration, my interest was to know the real scenario where custom iteration is quite handy.

最佳回答

当你所筹集的资源不是预先存储,而是按照每次储存的需要获得时,习俗分析员会很有用。 例如,在LINQ至Q中,当你做的是:

foreach(var employee in dataContext.Employees) {
    // Process employee
}

在<代码>foreach路程的每一步中,要求数据库中的雇员表格填写下一个记录。 这样,如果你越早越走,你就没有读到整个桌上,而只是需要的记录。

See here for a real-world example: e amountate Line on a file。 此处每一行均从<代码>foreach loop>每一版本的文档中读取。 (作为附带说明,《网络框架4》提供了同样的功能。)

问题回答

坦率地说,我不完全相信你会问什么,但如果你再次关切恢复阵列(如接头一遍)和返回原住者(摄取2)之间的区别,我就是回答。

返回一个阵列和返回一个发射器之间存在巨大差异。 回归阵列意味着恢复内部状态(除非你作为返回的一部分提供副本,但费用高昂)。 如果你回来,你只是回想,那将让电动人 state。 这也意味着,如果你需要的话,你可以进行zy评价(如果你重新回到一个阵列,你显然会这样做)。

此外,在C# 2号的主持人的支持下,确实没有理由以手头执行E数字。 请进一步详细了解Can person demystification the production keyword?

同样,过去习惯再生对我有用的另一个例子就是提供一种共同的抽象概念,在树木结构中重新列举所有节点。

foreach (Node n in tree)
  //...do something




相关问题