Return to Snippet

Revision: 13357
at April 20, 2009 22:59 by rengber


Updated Code
[TestMethod]
        public void DemoLINQExtensionMethods()
        {
            List<int> ints = new List<int>(new int[] { 1, 11, 12 });
            //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;
        }

Revision: 13356
at April 20, 2009 19:34 by rengber


Initial Code
[TestMethod]
        public void DemoLINQExtensionMethods()
        {
            List<int> ints = new List<int>(new int[]{1,11,12});
            int val = ints.FirstOrDefault(ThisOneGoesToEleven); 
            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; 
        }

Initial URL

                                

Initial Description
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.

Initial Title
LINQ Extension Methods

Initial Tags

                                

Initial Language
C#