Solution:NIIT/GNIIT Sonugiri0032@gmail.com

Saturday, January 02, 2016

Constructors and Its Types in C# With Examples

Constructors and Its Types in C# With Examples

What is  constructor?
A special method of the class that will be automatically invoked when an instance of the class is created is called a constructor. The main use of constructors is to initialize private fields of the class while creating an instance for the class. When you have not created a constructor in the class, the compiler will automatically create a default constructor in the class. The default constructor initializes all numeric fields in the class to zero and all string and object fields to null. 

Some of the key points regarding the Constructor are:
  • A class can have any number of constructors.
  • A constructor doesn't have any return type, not even void.
  • A static constructor can not be a parametrized constructor.
  • Within a class you can create only one static constructor. 
Constructors can be divided into 5 types:
  1. Default Constructor
  2. Parametrized Constructor
  3. Static Constructor
Now let us see  each constructor type with example as below
Default Constructor
A constructor without any parameters is called a default constructor; in other words this type of constructor does not take parameters. The drawback of a default constructor is that every instance of the class will be initialized to the same values and it is not possible to initialize each instance of the class to different values. The default constructor initializes:
  1. All numeric fields in the class to zero.
  2. All string and object fields to null.
Example
using System;
namespace
 DefaultConstractor
 {
    class addition
    {
        int a, b; 
        public addition()   //default contructor
        {
            a = 100;
            b = 175;
        }
 
        public static void Main()
        {
            addition obj = new addition(); //an object is created , constructor is called
            Console.WriteLine(obj.a);
            Console.WriteLine(obj.b);
            Console.Read();
        }
      }
    }
Parameterized Constructor 
 
A constructor with at least one parameter is called a parametrized constructor. The advantage of a parametrizedconstructor is that you can initialize each instance of the class to different values.
 
using System;
namespace Constructor
{
    class paraconstrctor    {
      public  int a, b;
      public paraconstrctor(int x, int y)  // decalaring Paremetrized Constructor with passing x,y parameter
        {
            a = x;
            b = y;
        }
   }
    class MainClass
    {
        static void Main()
        {
            paraconstrctor v = new paraconstrctor(100, 175);   // Creating object of Parameterized Constructor and passing values 
            Console.WriteLine("-----------parameterized constructor example by vithal wadje---------------");
            Console.WriteLine("\t");
            Console.WriteLine("value of a=" + v.a );
            Console.WriteLine("value of b=" + v.b);
            Console.Read();
        }
    }
}
Static Constructor
When a constructor is created as static, it will be invoked only once for all of instances of the class and it is invoked during the creation of the first instance of the class or the first reference to a static member in the class. A static constructor is used to initialize static fields of the class and to write the code that needs to be executed only once.

Some key points of a static constructor is:
  1. A static constructor does not take access modifiers or have parameters.
  2. A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
  3. A static constructor cannot be called directly.
  4. The user has no control on when the static constructor is executed in the program.
  5. A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.
Syntax
class employee
 {// Static constructor
  static employee(){}
 }
Now let us see it with practically
using System;
namespace staticConstractor
{
public class employee
{
    static employee() // Static constructor declaration{Console.WriteLine("The static constructor ");
}
public static void Salary()
 {
    Console.WriteLine();
    Console.WriteLine("The Salary method");
 }
}
class details
{
    static void Main()
    {
        Console.WriteLine("----------Static constrctor example by vithal wadje------------------");
        Console.WriteLine();
        employee.Salary();
        Console.ReadLine();
    }
  }
}
Summary
I hope this article is useful for all readers, if you have any suggestion then please contact me. 
Share:

Related Posts:

0 comments:

GNIITSOLUTION GNIIT SOLUTION. Powered by Blogger.

Translate

Blog Archive

undefined