您的位置:首页 > 其它

IEnumerable IEnumerator 分类: .NET 2014-05-13 17:38 350人阅读 评论(0) 收藏

2014-05-13 17:38 351 查看
看了官方的、网上其他的,写了2个demo,只不过还是没发现哪里会用到。

IEnumerable和IEnumerator都是System.Collections下的接口,结构分别如下:

namespace System.Collections
{
public interface IEnumerable
{
IEnumerator GetEnumerator();
}
}


namespace System.Collections
{
// Summary:
//     Supports a simple iteration over a nongeneric collection.
[ComVisible(true)]
[Guid("496B0ABF-CDEE-11d3-88E8-00902754C43A")]
public interface IEnumerator
{
// Summary:
//     Gets the current element in the collection.
//
// Returns:
//     The current element in the collection.
//
// Exceptions:
//   System.InvalidOperationException:
//     The enumerator is positioned before the first element of the collection or
//     after the last element.
object Current { get; }

// Summary:
//     Advances the enumerator to the next element of the collection.
//
// Returns:
//     true if the enumerator was successfully advanced to the next element; false
//     if the enumerator has passed the end of the collection.
//
// Exceptions:
//   System.InvalidOperationException:
//     The collection was modified after the enumerator was created.
bool MoveNext();
//
// Summary:
//     Sets the enumerator to its initial position, which is before the first element
//     in the collection.
//
// Exceptions:
//   System.InvalidOperationException:
//     The collection was modified after the enumerator was created.
void Reset();
}
}


Demo1:

class Program
{
static void Main(string[] args)
{
Book[] books = new Book[3] { new Book() { BookName = "java" }, new Book() { BookName = ".net" }, new Book() { BookName = "php" } };

BookShop bookShop = new BookShop(books);

//输出java .net php
foreach (Book book in bookShop)
{
Console.WriteLine(book.BookName);
}
Console.Read();
}
}

//此类没有实现IEnumerable,只实现GetEnumerator()方法即可,因为数组本身已经有鸟~
public class BookShop
{
public Book[] books = new Book[4];

public BookShop(Book[] books)
{
this.books = books;
}

public IEnumerator GetEnumerator()
{
return books.GetEnumerator();
}
}

public class Book
{
public string BookName { get; set; }
}


Demo2:

class Program
{
static void Main(string[] args)
{
StringArray strArray = new StringArray("java,.net,php");

//输出java .net php
foreach (string s in strArray)
{
Console.WriteLine(s);
}

Console.Read();
}
}

public class StringArray
{
public string[] strArray;

public StringArray(string str)
{
strArray = str.Split(',');
}

public IEnumerator GetEnumerator()
{
return new StringArrayEnumerator(strArray);
}
}

public class StringArrayEnumerator : IEnumerator
{
int position = -1;
string[] strArray;

public StringArrayEnumerator(string[] strArray)
{
this.strArray = strArray;
}

public bool MoveNext()
{
if (position < this.strArray.Length - 1)
{
position++;
return true;
}
else
{
return false;
}
}

public object Current
{
get { return strArray.ElementAt(position); }
}

public void Reset()
{
this.position = -1;//回到初始值
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: