c#列表和带有属性的枚举器 [英] c# list and enumerator with properties

查看:161
本文介绍了c#列表和带有属性的枚举器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题: 我有一个列表,并将字符串项添加到此列表中. 然后,我从列表中创建一个Enumerator.当我使用MoveNext()命令遍历该列表时,当我直接访问该枚举器时它将起作用.当我使用Enumerator属性访问枚举器时,它不起作用. MoveNext()命令不会增加索引.

I have the following problem: I have a list and add string items to this list. Then I create an Enumerator out of the list. When I loop through the list with the MoveNext() command it works when I access the enumerator directly. When I use the Enumerator properties to access the enumerator it doesn't work. The MoveNext() command doesn't increment the index.

这是我使用的代码.非常感谢您的帮助.

Here is the code I used. Thanks a lot for your help.

public class DummyKlasse
{
    public List<string>.Enumerator enumerator;
    public List<string> list;

    public DummyKlasse()
    {
        Console.WriteLine("Test");
        list = new List<string>();
        enumerator = new List<string>.Enumerator();
    }

    public List<string>.Enumerator Enumerator
    {
        get { return enumerator; }
        set { enumerator = value;}
    }

    public List<string> List
    {
        get { return list; }
        set { list = new List<string>(value); }
    }        

    public void dummyfunction()
    {
        List.Add("test1");
        List.Add("test2");

        enumerator = List.GetEnumerator();
    }
}

public class Program
{
    static void Main(string[] args)
    {
        DummyKlasse dummyklasse = new DummyKlasse();
        dummyklasse.dummyfunction();

        //Does not work with properties
        /*
        while (dummyklasse.Enumerator.MoveNext())
        {
            Console.WriteLine(dummyklasse.Enumerator.Current);
        }
        */
        //Works WITHOUT properties
        while (dummyklasse.enumerator.MoveNext())
        {
            Console.WriteLine(dummyklasse.enumerator.Current);
        }

        Console.ReadLine();
    }
}

推荐答案

是一个结构,因此在while循环中,每次访问Enumerator属性时,您都在调用getter,它将每次都为您返回一个新的枚举数副本.该枚举器将指向列表开头之前的项目,并且对MoveNextCurrent的每次调用都将在该枚举器的不同副本上进行.

List<T>.Enumerator is a struct, so in your while loop, each time you access the Enumerator property, you are calling the getter, which will return you a new copy of the enumerator each time. This enumerator will point to the item before th beginning of the list, and each call to MoveNext and Current will be done on a different copy of that enumerator.

作为非空列表的结果,MoveNext始终返回true,而Enumerator.Current始终为null.

As a result on a non-empty list, MoveNext will always return true, and Enumerator.Current is always null.

如果直接访问该字段,则避免使用此副本,而通过调用MoveNextCurrent可以访问同一枚举数.

If you access the field directly, you are avoiding this copy and the same enumerator being accessed by the calls to MoveNext and Current.

如果您将代码更改为:

using (var enumerator = dummyklasse.Enumerator)
{
    while (enumerator.MoveNext())
    {
        Console.WriteLine(enumerator.Current);
    }
}

它将按预期工作.请注意,这类似于foreach语句.

it will work as expected. Note this is similar to what the foreach statement does.

这篇关于c#列表和带有属性的枚举器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆