Csharp/C Sharp by API/System.Collections/IEnumerable — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 12:08, 26 мая 2010
Содержание
IEnumerable.DescendantNodes()
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);
}
}
}
IEnumerable.DescendantNodesAndSelf()
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);
}
}
}
IEnumerable.DescendantsAndSelf(String tagName)
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);
}
}
}
IEnumerable.GetEnumerator()
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);
}
}
}
implements IEnumerable
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 );
}
}
}