Public · Protected · Private
One such lambda expression
Type: Public  |  Created: 2012-08-13  |  Frozen: Yes
« Previous Public Blog Next Public Blog »
Comments
  • having a class below..

    public class Individual
    {
         public string FirstName { get; set; }
         public decimal pocketmoney { get; set; }
    }

     

    we have

                List<Individual> myfiends = new List<Individual>() {
        new Individual {FirstName = "john", pocketmoney = 555M},
        new Individual {FirstName = "rama", pocketmoney = 65M},
        new Individual {FirstName = "king", pocketmoney = 77M}
                };

    Create an extension method as below...

    namespace mynamespace
    {
        public static class MyExtensionMethods
        {
            public static IEnumerable<Individual> Filter(this IEnumerable<Individual> inds,
            Func<Individual, bool> selectorParam)
            {
                foreach (Individual ind in inds)
                {
                    if (selectorParam(ind))
                    {
                        yield return ind;
                    }
                }
            }
        }
    }
            

    now both following codes work the same way

    Func<Individual, bool> richnessFilter = delegate(Individual boy){ return boy.pocketmoney > 300; };
    IEnumerable<Individual> myrichfiriends = myfiends.Filter(richnessFilter);


    Func<Individual, bool> veryrichnessFilter = ind => ind.pocketmoney > 400;
    IEnumerable<Individual> myveryRichFriends = myfiends.Filter(veryrichnessFilter);

     

     

    2012-08-13 02:38
  • The Filter method needs a pointer to a method "that filters relavant condition"

    following two lines serve same purpose

    • delegate(Individual boy){ return boy.pocketmoney > 300; };
    • ind => ind.pocketmoney > 400;

    they support the extension method(that needs a pointer to such function).

    tehy are consumed by

    • IEnumerable<Individual> myrichfiriends = myfiends.Filter(richnessFilter);
    •  IEnumerable<Individual> myveryRichFriends = myfiends.Filter(veryrichnessFilter);

     

    2012-08-13 02:50
This blog is frozen. No new comments or edits allowed.