Pages

Wednesday, April 17, 2013

Extension Methods in c#

Extension Methods

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExtensionMethods
{
    class Program
    {
        static void Main(string[] args)
        {
            int x = 5;
            Console.WriteLine("Factorial of {0} number is{1}", x, x.fact());
            Console.ReadKey();
        }
    }
    static class factorial
    {
        public static int fact(this int number)
        {
            if (number <= 1) return 1;
            if (number == 2) return 2;
            else
            {
                return number * fact(number - 1);
            }
        }
    }
}

0 comments:

Post a Comment