Table of Contents

Class Switch<TSource, TResult>

Namespace
System.Linq
Assembly
Arc4u.dll

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

TSource

The type of the elements of the source sequence.

TResult

The 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

source IEnumerable<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

predicate Func<TSource, bool>

Returns true when the element belongs to this case.

selector Func<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

predicate or selector is 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

type Type

The type of the elements that belong to this case.

selector Func<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

type or selector is 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

selector Func<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

TCase

The type of the elements that belong to this case.

Exceptions

ArgumentNullException

selector is 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.