Introduction to Extension Methods

9/17/2008 8:00:00 PM size => 5.27 MB, duration => 7:24

Extension Methods are introduced in C# 3.0. Extension Methods are used to extend the behavior of the classes using methods.



 Customer customer = new Customer() { FirstName = "Mohammad", LastName = "Azam" };
                        
            Console.WriteLine(customer.ToJSON());

public static class ExtensionMethods
    {
        public static string ToJSON<T>(this T item) where T : BusinessBase
        {
            JavaScriptSerializer js = new JavaScriptSerializer();
            return js.Serialize(item);
        }

        public static bool IsPrimeNumber(this int no)
        {
            bool isPrime = true;

            if (no == 2) return false;

            for (int i = 2; i < no; i++)
            {
                if (no % i == 0)
                {
                    isPrime = false;
                    break;
                }
            }

            return isPrime;
        }
    }

Double Click on the Video to Enlarge

Bookmark and Share

Comments