Public · Protected · Private
C# Extension methods explained
Type: Public  |  Created: 2012-08-13  |  Frozen: Yes
« Previous Public Blog Next Public Blog »
Comments
  • public void myfunc()
    {
        int myInt = 0;
        string myString = "y";
        string[] myStrings = {myString, "janaganamana", "me", "you" };

        List<int> intList = new List<int> { myInt, 5,6,7 };

        Dictionary<string, int> myDict = new Dictionary<string, int> {
                                        { myString, myInt },
                                        { "janaganamana", 5 },
                                        { "me", 6 },
                                        { "you", 7 }
                            };

    }

    The above code may be very clear as a simple data types and initialization of same

    Now if I have two classes

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

    }
    public class Friends
    {
        public List<Individual> Group { get; set; }
    }

    we can initialize

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

    That part is no brainer.

    Now Let us use an extension method as explained earlier

    http://www.janaganamana.net/onedefault.aspx?bid=780
    1) Extension methods need to be defined at a top level namespace.
    2) class name does not matter much
    3) parameter has "this" with one argument

    namespace mynamespace
    {
        public static class MyExtensionMethods
        {

            public static decimal TotalInvestment(this Friends allfriends)
            {
                decimal total = 0;
                foreach (Individual boy in allfriends.Group)
                {
                    total += boy.pocketmoney;
                }
                return total;
            }
        }
    }

    Now you can call it as it it is a method of our friends class( notice"this Friends")  above

    decimal weHave = myfiends.TotalInvestment();decimal weHave = myfiends.TotalInvestment();

     

    2012-08-13 00:10
  • Update the Friends class as below.

    public class Friends : IEnumerable<Individual>
    {
        public List<Individual> Group { get; set; }
        public IEnumerator<Individual> GetEnumerator()
        {
            return Group.GetEnumerator();
        }
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }

     

    2012-08-13 00:29
  •  public static class MyExtensionMethods
    {
        public static decimal TotalInvestment(this IEnumerable<Individual> allfriends)
        {
            decimal total = 0;
            foreach (Individual boy in allfriends)
            {
                total += boy.pocketmoney;
            }
            return total;
        }


        public static IEnumerable<Individual> FilterByRichToday(
                this IEnumerable<Individual> inds, int atLeast)
        {

            foreach (Individual ind in inds)
            {
                if (ind.pocketmoney > atLeast)
                {
                    yield return ind;
                }
            }
        }
    }

    Now we can call as below ..
    decimal weHaveAlmost = myfiends.FilterByRichToday(1000).TotalInvestment();

    Notice ,, using extension methods, we did iteration inside our object also filteration where needed.

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