0% found this document useful (0 votes)
11 views4 pages

Code Listings 5

Uploaded by

mbsysde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views4 pages

Code Listings 5

Uploaded by

mbsysde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

11/16/24, 10:57 PM C# in a Nutshell - Code Listings

Chapter 7 - Collections
Enumeration
Low-level use of IEnumerable and IEnumerator

string s = "Hello";

// Because string implements IEnumerable, we can call GetEnumerator():


IEnumerator rator = s.GetEnumerator();

while (rator.MoveNext())
{
char c = (char) rator.Current;
Console.Write (c + ".");
}

Console.WriteLine();

// Equivalent to:

foreach (char c in s)
Console.Write (c + ".");

Disposing enumerators

IEnumerable<char> s = "Hello";

using (var rator = s.GetEnumerator())


while (rator.MoveNext())
{
char c = (char) rator.Current;
Console.Write (c + ".");
}

Use of nongeneric interfaces

void Main()
{
Count ("the quick brown fix".Split()).Dump();
}

public static int Count (IEnumerable e)


{
int count = 0;
foreach (object element in e)
{
var subCollection = element as IEnumerable;
if (subCollection != null)
count += Count (subCollection);
else
count++;
}
return count;
}

Simple iterator class

void Main()
{
foreach (int element in new MyCollection())
Console.WriteLine (element);
}

public class MyCollection : IEnumerable


{
int[] data = { 1, 2, 3 };

public IEnumerator GetEnumerator()


{
foreach (int i in data)
yield return i;
}
}

Simple iterator class - generic

void Main()
{
foreach (int element in new MyGenCollection())
Console.WriteLine (element);
}

public class MyGenCollection : IEnumerable<int>


{

https://w w w .albahari.com/nutshell/E12-CH07.aspx 1/4


11/16/24, 10:57 PM C# in a Nutshell - Code Listings
int[] data = { 1, 2, 3 };

public IEnumerator<int> GetEnumerator()


{
foreach (int i in data)
yield return i;
}

IEnumerator IEnumerable.GetEnumerator() // Explicit implementation


{ // keeps it hidden.
return GetEnumerator();
}
}

Iterator method

void Main()
{
foreach (int i in Test.GetSomeIntegers())
Console.WriteLine (i);
}

public class Test


{
public static IEnumerable <int> GetSomeIntegers()
{
yield return 1;
yield return 2;
yield return 3;
}
}

Low-level approach - nongeneric

void Main()
{
foreach (int i in new MyIntList())
Console.WriteLine (i);
}

public class MyIntList : IEnumerable


{
int[] data = { 1, 2, 3 };

public IEnumerator GetEnumerator() => new Enumerator (this);

class Enumerator : IEnumerator // Define an inner class


{ // for the enumerator.
MyIntList collection;
int currentIndex = -1;

internal Enumerator (MyIntList collection)


{
this.collection = collection;
}

public object Current


{
get
{
if (currentIndex == -1)
throw new InvalidOperationException ("Enumeration not started!");
if (currentIndex == collection.data.Length)
throw new InvalidOperationException ("Past end of list!");
return collection.data [currentIndex];
}
}

public bool MoveNext()


{
if (currentIndex >= collection.data.Length - 1) return false;
return ++currentIndex < collection.data.Length;
}

public void Reset() => currentIndex = -1;


}
}

Low-level approach - generic

void Main()
{
foreach (int i in new MyIntList())
Console.WriteLine (i);
}

class MyIntList : IEnumerable<int>


{
int[] data = { 1, 2, 3 };

https://w w w .albahari.com/nutshell/E12-CH07.aspx 2/4


11/16/24, 10:57 PM C# in a Nutshell - Code Listings
// The generic enumerator is compatible with both IEnumerable and
// IEnumerable<T>. We implement the nongeneric GetEnumerator method
// explicitly to avoid a naming conflict.

public IEnumerator<int> GetEnumerator() => new Enumerator(this);


IEnumerator IEnumerable.GetEnumerator() => new Enumerator(this);

class Enumerator : IEnumerator<int>


{
int currentIndex = -1;
MyIntList collection;

internal Enumerator (MyIntList collection)


{
this.collection = collection;
}

public int Current { get { return collection.data [currentIndex]; } }


object IEnumerator.Current { get { return Current; } }

public bool MoveNext() => ++currentIndex < collection.data.Length;

public void Reset() => currentIndex = -1;

// Given we don't need a Dispose method, it's good practice to


// implement it explicitly, so it's hidden from the public interface.

void IDisposable.Dispose() {}
}
}

ICollection and IList

Arrays

Lists, Queues, Stacks and Sets

Dictionaries

Customizable Collections and Proxies

Immutable Collections

Plugging in Equality and Order

Frozen Collections

C# 12
in a Nutshell
About the Book

Code Listings
C# 12 in a Nutshell
C# 10 in a Nutshell
C# 9.0 in a Nutshell
C# 8.0 in a Nutshell
C# 7.0 in a Nutshell

Extras

Contact

Buy print or Kindle edition

https://w w w .albahari.com/nutshell/E12-CH07.aspx 3/4


11/16/24, 10:57 PM C# in a Nutshell - Code Listings

Buy PDF edition

Read via O'Reilly subscription

https://w w w .albahari.com/nutshell/E12-CH07.aspx 4/4

You might also like