Feeds:
Posts
Comments

Archive for the ‘C#’ Category

Constructors in C# with Example

*PTR–    Point To Remember

A constructor is a method in the class which gets executed when its object is created is called as constructor.

Constructors can be classified into 5 types

  1. Default Constructor
  2. Parameterized Constructor
  3. Copy Constructor
  4. Static Constructor
  5. Private Constructor

Default Constructor : A constructor without any parameters is called as default constructor.

 Public ConstTest()

{

      //Default Constructor

}

PTR:  every instance of the class will be initialized to same values Not Different values

Parameterized Constructor :A constructor with at least one parameter is called as parameterized constructor.

Public ConstTest (Int X ,Int Y)

{

      //Parameterized Constructor

}

PTR: you can initialize each instance of the class to different values

Copy Constructor: A parameterized constructor that contains a parameter of same class type is called as copy constructor.

Public ConstTest ()

{

      //Default Constructor

}

 

Public ConstTest (ConstTest T )

{

      //Copy Constructor

          // Access The property of T.name

}

PTR: initialize new instance to the values of an existing instance.

Static Constructor : You can create a constructor as static and when a constructor is created as static

Static  ConstTest ()

{

      //Static Constructor

}

PTR: it will be invoked only once for any number of instances of the class and it is during the creation of first instance of the class

Private Constructor :You can also create a constructor as private. When a class contains at least one private constructor

Private  ConstTest ()

{

      //private Constructor

}

PTR: restrict the class from being instantiated

Advertisements

Read Full Post »

What are extension methods?

What are extension methods?

This method allows us to add a new static method to the existing classes.

Example

class Program
    {
        static void Main(string[] args)
        {
            int x = 3;
            Console.WriteLine(x.factorial());
            Console.ReadLine();
        }
    }
    public static class MyMathExtension
    {
        public static int factorial(this int x)
        {
            if (x <= 1) return 1;
            if (x == 2) return 2;
            else
                return x * factorial(x – 1);
        }
    }
Use Of This

  1.     Extension methods allow existing classes to be extended without relying on inheritance or having to change the class’s source code.
  2.     If the class is sealed than there in no concept of extending its functionality. For this a new concept is introduced i.e. extension methods.
  3.     This feature is important for all developers especially if you would like to use the dynamism of the C# enhancements to be taken place in your classes design.

Important points while using extension methods:

  1.     An extension method must be defined in a top-level static class.
  2.     An extension method with the same name and signature as an instance method will not be called.
  3.     Extension methods cannot be used to override existing methods.
  4.     The concept of extension methods cannot be applied to fields, properties or events.
  5.     Overuse of extension methods is not good style of programming.

Read Full Post »