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

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;
        }
    }

[Play the Video]