/ Published in: C#
LINQ Extension method called with delegate, anonymous delegate, and lambda delegate.
Note that this only returns a "default" in the sense that it does not throw an exception if no item in the source matches the selection criteria. .First() does throw an exception in this case.
Note that this only returns a "default" in the sense that it does not throw an exception if no item in the source matches the selection criteria. .First() does throw an exception in this case.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
[TestMethod] public void DemoLINQExtensionMethods() { //Standard Delegate int val = ints.FirstOrDefault(ThisOneGoesToEleven); Assert.IsTrue(val == 11, "Eleven was not found. Value = " + val.ToString()); //Using Anonymous Method val = ints.FirstOrDefault(delegate(int i) { return i == 11; }); Assert.IsTrue(val == 11, "Eleven was not found. Value = " + val.ToString()); //Using equivalent Lambda syntax val = ints.FirstOrDefault(x => x == 11); Assert.IsTrue(val == 11, "Eleven was not found. Value = " + val.ToString()); } private bool ThisOneGoesToEleven(int volume) { return volume == 11; }