/ 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.
Expand |
Embed | Plain Text
[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; }
You need to login to post a comment.
