Sunday 31 January 2016

What is extension methods and how to use them ?




 An extension method is a static method of a static class that can be invoked using the instance method syntax. Extension methods are used to add new behaviors to an existing type without altering. In extension method "this" keyword is used with the first parameter and the type of the first parameter will be the type that is extended by extension method.
Conceptually, extension methods provides as an implementation of the decorator structural pattern. At compile time an extension method call is translated into an ordinary static method call.


Example:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace DempApp
{

    public static class MyExtensions
    {
        public static int WordCount(this String str)
        {
            return str.Split(new char[] { ' ', '.', ',' }).Length;
        }
    }

    class Program
    {
        public static void Main()
        {
            string s = "WCF WPF and DotNet trating program";

            //calling extension method
            int i = s.WordCount();

            Console.WriteLine(i);
            Console.ReadKey();
        }
    }
}


 Notes:-
  • An extension method is defined as static method but it is called like as instance method.
  • An extension method first parameter specifies the type of the extended object, and it is preceded by the "this" keyword.
  • An extension method having the same name and signature like as an instance method will never be called since it has low priority than instance method.
  • An extension method couldn't override the existing instance methods.
  • An extension method cannot be used with fields, properties or events.
  • The compiler doesn't cause an error if two extension methods with same name and signature are defined in two different namespaces and these namespaces are included in same class file using directives. Compiler will cause an error if you will try to call one of them extension method.





Use of Private Constructor in C# with Example



Private Constructor

Private constructor is a special instance constructor used in a class that contains static member only. If a class has one or more private constructor and no public constructor then other classes is not allowed to create instance of this class this mean we can neither create the object of the class nor it can be inherit by other class. The main purpose of creating private constructor is used to restrict the class from being instantiated when it contains every member as static.


Example:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Threading;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;

namespace DempApp
{

    public class Sample
    {
        public string var1, var2;
        public Sample(string a, string b)
        {
            var1 = a;
            var2 = b;
        }
        private Sample()  // Private Constructor Declaration
        {
            Console.WriteLine("Private Constructor with no prameters");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            // Here we don't have chance to create instace for private constructor
           // Sample obj = new Sample();     //Error

            Sample obj1 = new Sample("Welcome", "to rpsingh");
            Console.WriteLine(obj1.var1 + " " + obj1.var2);
            Console.ReadLine();
        }
    }

}


Note:-

In above method we can create object of class with parameters will work fine. If create object of class without parameters it will not allow us create.


Important points of private constructor

   1.   One use of private construct is when we have only static member.
   2.   Once we provide a constructor that is either private or public or any, the compiler will not allow us to add public constructor without parameters to the class.
    3.  If we want to create object of class even if we have private constructors then we need to have public constructor along with private constructor