Wednesday, March 19, 2014

.Single versus .First in C# lambda expressions

int[] array1 = { 1, 3, 1000, 4, 5 };
int value1 = array1.Single(element => element > 999);

...is an example given here of using .Single in a lambda expression. When using .Single one is to get back one and only one item. There is an assumption that only one item may match the criteria. An exception will be thrown if this is not the case. .First in contrast will return the first match even if there is more than one match.

No comments:

Post a Comment