Csharp/C Sharp by API/System.Collections/IEnumerable
Содержание
IEnumerable.DescendantNodes()
<source lang="csharp"> using System; using System.Linq; using System.Collections; using System.Collections.Generic; using System.Xml; using System.Xml.Linq; public class MainClass {
public static void Main() { XDocument xDocument = new XDocument( new XElement("Books", new XElement("Book", new XAttribute("type", "Author"), new XComment("This is a new author."), new XElement("FirstName", "A"), new XElement("LastName", "B")), new XElement("Book", new XAttribute("type", "Author"), new XElement("FirstName", "C"), new XElement("LastName", "D")))); IEnumerable<XElement> elements = xDocument.Element("Books").Elements("Book"); foreach (XElement element in elements) { Console.WriteLine("Source element: {0} : value = {1}", element.Name, element.Value); } foreach (XNode node in elements.DescendantNodes()) { Console.WriteLine("Descendant node: {0}", node); } }
}
</source>
IEnumerable.DescendantNodesAndSelf()
<source lang="csharp"> using System; using System.Linq; using System.Collections; using System.Collections.Generic; using System.Xml; using System.Xml.Linq; public class MainClass {
public static void Main() { XDocument xDocument = new XDocument( new XElement("Books", new XElement("Book", new XAttribute("type", "Author"), new XComment("This is a new author."), new XElement("FirstName", "A"), new XElement("LastName", "B")), new XElement("Book", new XAttribute("type", "Author"), new XElement("FirstName", "C"), new XElement("LastName", "D")))); IEnumerable<XElement> elements = xDocument.Element("Books").Elements("Book"); foreach (XElement element in elements) { Console.WriteLine("Source element: {0} : value = {1}", element.Name, element.Value); } foreach (XNode node in elements.DescendantNodesAndSelf()) { Console.WriteLine("Descendant node: {0}", node); } }
}
</source>
IEnumerable.DescendantsAndSelf(String tagName)
<source lang="csharp"> using System; using System.Linq; using System.Collections; using System.Collections.Generic; using System.Xml; using System.Xml.Linq; public class MainClass {
public static void Main() { XDocument xDocument = new XDocument( new XElement("Books", new XElement("Book", new XAttribute("type", "Author"), new XComment("This is a new author."), new XElement("FirstName", "A"), new XElement("LastName", "B")), new XElement("Book", new XAttribute("type", "Author"), new XElement("FirstName", "C"), new XElement("LastName", "D")))); IEnumerable<XElement> elements = xDocument.Element("Books").Elements("Book"); foreach (XElement element in elements) { Console.WriteLine("Source element: {0} : value = {1}", element.Name, element.Value); } foreach (XElement element in elements.DescendantsAndSelf("LastName")) { Console.WriteLine("Descendant element: {0}", element); } }
}
</source>
IEnumerable.GetEnumerator()
<source lang="csharp"> using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Text; public class MainClass {
public static void Main() { IEnumerable<string> enumerable = new string[] { "A", "B", "C" }; foreach (string s in enumerable) Console.WriteLine(s); IEnumerator<string> enumerator = enumerable.GetEnumerator(); while (enumerator.MoveNext()) { string s = enumerator.Current; Console.WriteLine(s); } }
}
</source>
implements IEnumerable
<source lang="csharp"> using System; using System.Collections; using System.Collections.Generic; public class MyColl<T> : IEnumerable<T> {
private T[] items; public MyColl( T[] items ) { this.items = items; } public IEnumerator<T> GetEnumerator() { foreach( T item in items ) { yield return item; } } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); }
} public class Test {
static void Main() { MyColl<int> integers = new MyColl<int>( new int[] {1, 2, 3, 4} ); foreach( int n in integers ) { Console.WriteLine( n ); } }
}
</source>