.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

About C# Constructors

Posted By:Krishna       Posted Date: July 20, 2013    Points: 200    Category: C#    URL: http://www.dotnetspark.com  

Hi all here I want to explain about C# Constructors, so lets us know first what is Constructors why are we using Constructors and different types of Constructors.
 

C# Constructors

Hi all here  I want to explain about C# Constructors, so lets us know first what is Constructors why are we using Constructors and different types of Constructors.

What is Constructor?

A constructor is nothing but method where this method name is same as  class name and should not have any written type not even void.

 

Constructors are used for creating instance of class.

 

Different types of Constructors

  • Default Constructors.
  • Parameter Constructors.

 

Does C# class will compile and run with out creating constructor explicitly ?

Yes ,you can check by exe with below program.

class Program
    {
        static void Main(string[] args)
        {
            ConstrcutureExample _defaultConstructureObject = new ConstrcutureExample();
            _defaultConstructureObject.EmployeeName = "NithyaSree";
            _defaultConstructureObject.EmployeeSalary = 350000;
            _defaultConstructureObject.EmployeeAddress = "Bangalore ITPL";
            Console.WriteLine(_defaultConstructureObject.EmployeeName);
            Console.WriteLine(_defaultConstructureObject.EmployeeSalary);
            Console.WriteLine(_defaultConstructureObject.EmployeeAddress);
            Console.Read();

        }
    }
public class ConstrcutureExample
    {
        public string EmployeeName = string.Empty;
        public decimal EmployeeSalary = 0.0m;
        public string EmployeeAddress = string.Empty;
        public ConstrcutureExample() { }
        public ConstrcutureExample(string Ename, decimal Esalary, string Eaddress)//Parameterized constructure
        {
            EmployeeName = Ename;
            EmployeeSalary = Esalary;
            EmployeeAddress = Eaddress;
        }
    }


If we observer above program where we didn't write any constructure for creating an object in ConstrcutureExample class but still  I used ConstrcutureExample() constructure for creating object in main class how is it possible?

 Ok lets go little deeper now,when above program compiles by c# compailer (.csc)compiler when a class didn't have any constructure either parameter constructure(or) parameter less constructure compailer will create default constructor before converting class to MSIL(or) IL.Hence we can make use of this default constructore for creating instance of class.

 We can check is default constructor is created in dll(MSIL) using ildasm.exe

 You can find ildasm.exe in specified path "C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin"

 If you explore dll using ildasm.exe we can observer .ctor constructor created in our dll.

So now you would have basic idea of constructors ,lets see now about parameter constructor and use of this parameter constructor.

 What is parameter constructor?

The constructor where we can pass parameters is nothing but parameter constructor, main purpose of parameter constructor is to initialize the class members while creating instance of class it self.

 


See below program

class Program
    {
        static void Main(string[] args)
        {

            ConstrcutureExample _defaultConstructureObject = new ConstrcutureExample();
            _defaultConstructureObject.EmployeeName = "NithyaSree";
            _defaultConstructureObject.EmployeeSalary = 350000;
            _defaultConstructureObject.EmployeeAddress = "Bangalore ITPL";
            Console.WriteLine(_defaultConstructureObject.EmployeeName);
            Console.WriteLine(_defaultConstructureObject.EmployeeSalary);
            Console.WriteLine(_defaultConstructureObject.EmployeeAddress);
            Console.Read();

            //Creating instance using parameter constructure
            ConstrcutureExample _parameterConstructureObject = new ConstrcutureExample("NickyKutti", 350000, "Bangalore ITPL");
            Console.WriteLine(_parameterConstructureObject.EmployeeName);
            Console.WriteLine(_parameterConstructureObject.EmployeeSalary);
            Console.WriteLine(_parameterConstructureObject.EmployeeAddress);
            Console.Read();
        }
    }
    public class ConstrcutureExample
    {
        public string EmployeeName = string.Empty;
        public decimal EmployeeSalary = 0.0m;
        public string EmployeeAddress = string.Empty;
        public ConstrcutureExample() { }
        public ConstrcutureExample(string Ename, decimal Esalary, string Eaddress)//parameter constructure
        {
            EmployeeName = Ename;
            EmployeeSalary = Esalary;
            EmployeeAddress = Eaddress;
        }
    }

Note: if you are declare parameter constructor c# compiler will not create default constructor so if you want to use default constructor for creating object you need to declare explicitly. 


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend