IEnumerator<T>
and IEnumerable<T>
**IEnumerable<T>**
which supports iterating over the collection.**foreach**
statement we iterate over an array of elements.
**foreach**
statement, however.**IEnumerator<T>**
and **IEnumerator**
interfaces are designed to enable the iterator pattern for iterating over collections of elements, rather than the length-index pattern.**foreach**
inside another, both using the same collection) the collection must maintain a state indicator of the current element so that when **MoveNext()**
is called, the next element can be determined.
https://s3-us-west-2.amazonaws.com/secure.notion-static.com/ca76f803-e06c-4667-bacf-a8912674850e/Untitled
**IEnumerator<T>**
and **IEnumerator**
interfaces directly.**IEnumerable<T>**
whose only method is **GetEnumerator()**
.
IEnumerator<T>
.IEnumerator<T>
and will keep the state of the iteration loop.var stack = new Stack<int>();
Stack<int>.Enumerator enumerator = stack.GetEnumerator();
while (enumerator.MoveNext())
{
int number = enumerator.Current;
Console.WriteLine(number);
}
Range()
static method generates a range of integers from a given start value and for a given count.var numbers = Enumerable.Range(1, 10).ToList();
**Repeat()**
static method generates a sequence of repeated occurrences of a given element.var repeatedValues = Enumerable.Repeat("Hello World!", 5).ToArray();
ICollection<T>
and IReadOnlyCollection<T>
**ICollection<T>**
includes two main members: Count
and CopyTo()
.**ICollection<T>**
does not include an index.**CopyTo()**
method provides the ability to convert the collection into an array.
**CopyTo()**
requires to initialize the target array with sufficient capacity.interface ICollection<T> : IEnumerable<T>, IEnumerable
{
int Count { get; }
bool IsReadOnly { get; }
void Add(T item);
void Clear();
bool Contains(T item);
void CopyTo(T[] array, int arrayIndex);
bool Remove(T item);
}
interface IReadOnlyCollection<T> : IEnumerable<T>, IEnumerable
{
int Count { get; }
}
Add()
calls are generated by the compiler rather than explicitly coded.ICollection<T>
or simply have one or more Add()
methods exist on a type that implements IEnumerable<T>
.
Add()
method needs to take parameters that is compatible with the values specified in the collection initializer.new[]
.var sevenWorldBlunders = new List<string>()
{
"Wealth without work",
"Pleasure without conscience"
};
var items = new [] // new string[]
{
"Item1",
"Item2"
};
Array
Array
class is not part of the System.Collections
namespaces.**IList**
interface.public abstract class Array : ICloneable, System.Collections.IList,
System.Collections.IStructuralComparable, System.Collections.IStructuralEquatable
System.Collections
namespaces, Array has a fixed capacity.
IList<T>
, ICollection<T>
, IEnumerable<T>
, IReadOnlyList<T>
and IReadOnlyCollection<T>
.