Skip to content

Instantly share code, notes, and snippets.

@usametov
Created April 20, 2021 17:08
Show Gist options
  • Save usametov/cfe1b7a437588efabdbda730e4f342c3 to your computer and use it in GitHub Desktop.
Save usametov/cfe1b7a437588efabdbda730e4f342c3 to your computer and use it in GitHub Desktop.
cool pattern matching in modern C#
public static T ExhaustiveExample<T>(IEnumerable<T> sequence) =>
sequence switch
{
System.Array { Length : 0 } => default(T),
System.Array { Length : 1} array => (T)array.GetValue(0),
System.Array {Length : 2} array => (T)array.GetValue(1),
System.Array array => (T)array.GetValue(2),
IEnumerable<T> list
when !list.Any() => default(T),
IEnumerable<T> list
when list.Count() < 3 => list.Last(),
IList<T> list => list[2],
null => throw new ArgumentNullException(nameot(sequence)), => sequence.Skip(2).First(),
_ => sequence.Skip(2).First()
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment