Class Switch<TSource, TResult>
A lazily evaluated "switch" over a sequence: each element of the source is tested against the registered cases, in registration order, and the first matching case produces the result for that element. Elements that match no case are skipped.
public class Switch<TSource, TResult> : IEnumerable<TResult>, IEnumerable
Type Parameters
TSourceThe type of the elements of the source sequence.
TResultThe type of the produced elements.
- Inheritance
-
Switch<TSource, TResult>
- Implements
-
IEnumerable<TResult>
- Inherited Members
- Extension Methods
Examples
var labels = new object[] { 1, "text", 2.5 }
.Case<object, string>(x => x is int, x => "an int")
.Case(typeof(string), x => "a string")
.ToList(); // ["an int", "a string"]: 2.5 matches no case and is skipped.
Constructors
Switch(IEnumerable<TSource>)
A lazily evaluated "switch" over a sequence: each element of the source is tested against the registered cases, in registration order, and the first matching case produces the result for that element. Elements that match no case are skipped.
public Switch(IEnumerable<TSource> source)
Parameters
sourceIEnumerable<TSource>The sequence to project.
Examples
var labels = new object[] { 1, "text", 2.5 }
.Case<object, string>(x => x is int, x => "an int")
.Case(typeof(string), x => "a string")
.ToList(); // ["an int", "a string"]: 2.5 matches no case and is skipped.
Methods
Case(Func<TSource, bool>, Func<TSource, TResult>)
Adds a case selected by a predicate.
public Switch<TSource, TResult> Case(Func<TSource, bool> predicate, Func<TSource, TResult> selector)
Parameters
predicateFunc<TSource, bool>Returns true when the element belongs to this case.
selectorFunc<TSource, TResult>Produces the result for an element that belongs to this case.
Returns
- Switch<TSource, TResult>
The same Switch<TSource, TResult>, to chain calls.
Exceptions
- ArgumentNullException
predicateorselectoris null.
Case(Type, Func<TSource, TResult>)
Adds a case selected by type: the case applies to the elements whose runtime type is assignable to type.
public Switch<TSource, TResult> Case(Type type, Func<TSource, TResult> selector)
Parameters
typeTypeThe type of the elements that belong to this case.
selectorFunc<TSource, TResult>Produces the result for an element that belongs to this case.
Returns
- Switch<TSource, TResult>
The same Switch<TSource, TResult>, to chain calls.
Exceptions
- ArgumentNullException
typeorselectoris null.
Case<TCase>(Func<TCase, TResult>)
Adds a case selected by type: the case applies to the elements that are a TCase, and the selector receives them already converted to that type.
public Switch<TSource, TResult> Case<TCase>(Func<TCase, TResult> selector)
Parameters
selectorFunc<TCase, TResult>Produces the result for an element that belongs to this case.
Returns
- Switch<TSource, TResult>
The same Switch<TSource, TResult>, to chain calls.
Type Parameters
TCaseThe type of the elements that belong to this case.
Exceptions
- ArgumentNullException
selectoris null.
GetEnumerator()
Enumerates the source and returns, for each element matching a case, the result of the first matching case.
public IEnumerator<TResult> GetEnumerator()
Returns
- IEnumerator<TResult>
An enumerator over the results.