Created
April 20, 2021 17:08
-
-
Save usametov/cfe1b7a437588efabdbda730e4f342c3 to your computer and use it in GitHub Desktop.
cool pattern matching in modern C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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