C#:IEnumerable,GetEnumerator,一个简单,简单的例子,请!

时间:2010-05-18 19:36:32

标签: c# generics list dictionary

尝试创建一个实现get枚举器的uebersimple类,但由于缺少简单/无功能的示例而导致失败。我想做的就是在数据结构周围创建一个包装器(在这种情况下是一个列表,但我以后可能需要一个字典)并添加一些函数。

public class Album
{
    public readonly string Artist;
    public readonly string Title;
    public Album(string artist, string title)
    {
         Artist = artist;
         Title = title;
    } 
}
public class AlbumList
{
    private List<Album> Albums = new List<Album>;
    public Count { get { return Albums.Count; } }

    .....
    //Somehow GetEnumerator here to return Album
}

谢谢!

4 个答案:

答案 0 :(得分:36)

您只需返回List&lt; T&gt;返回的枚举器.GetEnumerator:

public class AlbumList : IEnumerable<Album>
{
    // ...

    public IEnumerator<Album> GetEnumerator()
    {
        return this.albums.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}

答案 1 :(得分:15)

除了其他答案之外,如果您需要更多地控制枚举器的工作方式,或者是否需要在基础数据结构提供的范围之外进行自定义,那么您可以使用yield关键字。

public class AlbumList : IEnumerable<Album>
{
  public IEnumerator<Album> GetEnumerator()
  {
    foreach (Album item in internalStorage)
    {
      // You could use conditional checks or other statements here for a higher
      // degree of control regarding what the enumerator returns.
      yield return item;
    }
  }
}

答案 2 :(得分:9)

using System.Collections;
using System.Collections.Generic;

public class AlbumList : IEnumerable<Album>
{
    private List<Album> Albums = new List<Album>();

    public int Count { get { return Albums.Count; } }

    public IEnumerator<Album> GetEnumerator()
    {
        return this.Albums.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return this.GetEnumerator();
    }
}

或简化版:

public class AlbumList
{
    private List<Album> Albums = new List<Album>();

    public int Count { get { return Albums.Count; } }

    public IEnumerator<Album> GetEnumerator()
    {
        return this.Albums.GetEnumerator();
    }
}

我不建议省略IEnumerable<T>接口,因为您放弃了与.NET的集成,例如使用LINQ的可能性,但您可以使用C#中的foreach迭代集合。 / p>

或者这个更短: - )

public class AlbumList : List<Album>
{
}

当然最后一个是一个可变列表,可能不是你想要的。

答案 3 :(得分:2)

根据你的评论你希望有一个数据结构(列表)的包装器,以及一个返回一个Album的枚举器函数,我认为你在谈论索引器属性,对吧?你就是这样做的:

public class Album
{
    public readonly string Artist;
    public readonly string Title;
    public Album(string artist, string title)
    {
         Artist = artist;
         Title = title;
    } 
}

public class AlbumList
{
    private List<Album> Albums = new List<Album>();
    public int Count
    {
        get { return Albums.Count; }
    }

    public Album this[int index]
    {
        get
        {
            return Albums[index];
        }
    }

    public Album this[string albumName]
    {
        get
        {
            return Albums.FirstOrDefault(c => c.Title == albumName);
        }
    }

    public void Add(Album album)
    {
        Albums.Add(album);
    }

    public void Remove(Album album)
    {
        Albums.Remove(album);
    }
}

小型控制台程序:

        AlbumList albums = new AlbumList();
        albums.Add(new Album { Artist = "artist1", Title = "title1" });
        albums.Add(new Album { Artist = "artist2", Title = "title2" });

        for (int i = 0; i < albums.Count; i++)
        {
            Console.WriteLine(albums[i].Artist);
            Console.WriteLine(albums[i].Title);
        }

        Console.WriteLine("title for artist1");
        Console.WriteLine(albums["artist1"].Title);