Sunday, August 23, 2020

constructor in c# || default constructor in c#

 Constructor in c# || Default constructor in c#


constructor in c# || default constructor in c#
constructor in c# || default constructor in c#

A constructor is a special kind of function or method that is used to initialize objects.it is called whenever object of a class is created. 


Constructor is generally used to initialize the data members of new object. you know, whenever i declare variable 

like string Name,int Age etc. so constructor is generally initialize the default value in those variables like

in String Name initialize null or empty and in int Age having initialize 0 value.


constructor in c# || default constructor in c#
constructor in c# || default constructor in c#


How to Create Constructor:


Constructor name should be class name and it can also hold parameter. Constructor can be public,private.

As per above discussion Constructor is automatically call whenever an object of class is created.that's why we are not going to call constructor explicitly.

e.g:

let see 

Public Class clsEmployee

{

    // Creation of Constructor


   Public clsEmployee()  // So this is called constructor

   {

      // You know, this constructor is called default constructor.

   }


Public Static Void Main()

{

   // Here Constructor is automatically called.

    clsEmployee ObjEmp=new clsEmployee() ;

}




}

constructor in c# || default constructor in c#
constructor in c# || default constructor in c#
constructor in c# || default constructor in c#

Types of constructor in c#

1. Default Constructor

2. Private Constructor

3. Parameterize Constructor

4. Copy Constructor

5. Static Constructor


private constructor in c#

We know that Constructor is special kind of function which is used to initialize the default value. Whenever constructor mark as a private in a class 

we are not able to create an object outside the class. don't worry if you do not understand , i will explain it.We know that Constructor name should be class name and it is called automatically.

let see:

Public class ClsEmployee

{

   private ClsEmployee()

   {

  // This is private Constructor

   }

  

}


Public Class ClsHR

{

  

  Public static Void Main()

  {

    ClsEmployee ObjEmp=new ClsEmployee();  

  }

}

private constructor in c#
private constructor in c#


Now we have got compile time error like : 

'ClsEmployee.ClsEmployee()' is inaccessible due to its protection level. 

private constructor in c#
private constructor in c#

So , If any class having multiple private constructor and not any public constructor is available then we are not able to create an object outside the class, keep in mind.

If we want to create an object of that class which contains private constructor then we have to need at least create one public constructor also.


Note: If Constructor is not marked as public or private. It is by default private and prevents to creation of an object outside the class.


Points:

1. If constructor mark as private class not allowed to create an object outside the class.

2. Private constructor not allowed or support the inheritance.

3. Use private constructor when class have only static members.

4. Private Constructor implements of a singleton class pattern.

5. If we want to create an object of that class which contains private constructor then we have to need at least create one public constructor also.


parameterized constructor in c#

A Constructor contain at least one parameter is called parameterize constructor. parameterize constructor can initialize every instance of the class to 

different values.

Let see:

Public Class ClsEmployee

{

    string UserName=string.Empty;

    int Id=0;

    ClsEmployee(String Name,Int UserId)

   {

     UserName=Name;

     Id=UserId;

   }


  Public static void Main()

  {

    ClsEmployee ObjEmp=new ClsEmployee("Dheeraj Kumar",12);

    Console.WriteLine(Objemp.UserName);

    Console.WriteLine(Objemp.Id);

  }


}

parameterized constructor in c#
parameterized constructor in c#

Output:

parameterized constructor in c#
parameterized constructor in c#


Copy constructor in c#

A Constructor which is creates an object by copying variables from another object known as copy constructor.It is also parameterize constructor.

Normally in C# not provides a copy constructor for an object but if you want to create a copy constructor follow these step:


public class ClsEmployee

{

    string Name = string.Empty;

    int id = 0;

   public ClsEmployee(ClsEmployee ObjEmp)

    {

        Name = ObjEmp.Name;

        id = ObjEmp.id;

    }

    public ClsEmployee(string Name, int Id)

    {

        this.Name = Name;

        this.id = Id;

    }


    public string GetData

    {

        get {

            return "Name: " + Convert.ToString(Name) + " Id: " + id;

        }

    }

    public static void Main()

    {

        ClsEmployee Objemployee = new ClsEmployee("Dheeraj Kumar",12);

        ClsEmployee Objemplo = new ClsEmployee(Objemployee);

        Console.WriteLine(Objemplo.GetData);

        Console.ReadLine();

    }

}

copy constructor in c#
copy constructor in c#


Output:

copy constructor in c#
copy constructor in c#

Static Constructor:


If constructor mark as a static  known as static constructor. It is called only once in the class at during the creation of the first refrence to static

member in the class.

Static constructor is initialize static data and static fields of the class and to be invoked only once.


Points:

1. we have not control over Static Constructor.

2. It can not be called explicitly.

3. Static Constructor does not take access modifier like public or private.

4. Static Constructor does not contain parameter and that's why static constructor is not support inheritance model.


let see:



public static class ClsCompany

{

    static string name;

     static ClsCompany()

    {

        name = "Dheeraj";

        Console.WriteLine("Hello "+ name);

        Console.ReadLine();

    }


    public static void Main()

    {


    }

}

static constructor in c#
static constructor in c#

static constructor in c# not allowed parameter

static constructor in c#
static constructor in c#
static constructor in c#
static constructor in c#

 Output

static constructor in c#
static constructor in c#

I recommended all oops Concept are given below, you are going to learn one by one: 

  1. Class  And Namespace in C#
  2. Object
  3. Constructor
  4. Variables
  5. Static Class
  6. Static Constructor
  7. Static Method
  8. Enacpuslation
  9. Abstraction
  10. Abstact Class
  11. Abstract Method
  12. Interface
  13. Private Constructor
  14. Sealed Class
  15. Inheritance
  16. Polymorphism
  17. Method Hiding 
  18. Method Overloading
  19. Method Overriding 
  20. Compile Time Polymorphism
  21. Run time Polymorphism
  22. Difference between constructor and Static Constructor  



No comments:

Post a Comment

If you have any query kindly let me know.

Blazor drawback| drawback of blazor| Disadvantage of blazor in c#

  Blazor drawback| drawback of blazor| Disadvantage of blazor in c# While Blazor offers many advantages, it also has a few drawbacks to cons...