Constructor in C#

 # Constructor:-

1. Constructor is a special type of method which automatically called at the time of object creation.

 

# property of constructor

1. The class and method name are similarly.

2. Constructor only uses  public specifier.

3. Constructor does not used any  type (ex void, int , double etc ).

# Types of constructor

1. Default constructor

2. Parameterized  constructor

3. Constructor with default argument

4. Copy constructor – memory management no

Q. Why we use constructor?

Ans- For faster execution of code is done using constructor.

 

1. Default Constructor:-  If the class name and method name are similar then it is called default constructor.

 

Ex. using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace defaultconstructor

{

    class student

    {

        public student()  // constructor

        {

 

            Console.WriteLine("Welcome");

 

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            student s1 = new student();

            Console.ReadKey();

 

        }

    }

}

2 . parameterized constructor :-  is used to  parameter inside the constructor method .

Ex.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace defaultconstructor

{

    class student

    {

        public student(int a )  //  parameterized constructor

        {

 

            Console.WriteLine(a);

 

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            student s1 = new student(30);

            Console.ReadKey();

 

        }

    }

}

3.Constructor with default argument:- The default argument is used to store default data value inside the constructor method.

Ex. using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace defaultconstructor

{

    class student

    {

        public student(int a=10 )  //  a is called default argument

            // constructor with default argument

        {

 

            Console.WriteLine(a);

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            student s1 = new student(30); // variable overridding

            Console.ReadKey();

        }

    }

}

5.  Mutiple constructor:- it  is a combination of default and parameterized constructor

Ex. using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace defaultconstructor

{

    class student

    {

        public student(int a=10 )   

            

        {

            Console.WriteLine(a);

 

 

            

 

        }

 

 

        public student()

        {

            Console.WriteLine("welcome ");

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            student s1 = new student(20);

            student s2 = new student();

            Console.ReadKey();

 

        }

    }

}

Comments

Popular posts from this blog

ViewState for state management

Store procedure Data Base handling (procedure_insert).

Database joining