We provide IT Services

Register Now Click On

Read More Tutorials Click On

Post Page Advertisement [Top]


What is constructor in c#

Constructor is a special kind of method in c#. Constructor are create same as the class name. constructor never be return type, 

Every class created by default constructor. 

constructor can be parameterized. 

constructor can be overloaded.

We can create more then one constructor in a class. 

How many Types of Constructor:-

There are Five types of Constructor.

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

Syntax of Constructor:-

Using System;
Class Rock
{
Public Rock()------------>This is the default constrator
{
/////------------------////////
}
}

Example of Constructor:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace what_is_constructor
{
    class RockProgrammer
    {
        int a = 50, b = 30, c;

        public RockProgrammer()
        {
            c = a + b;
            Console.WriteLine(c);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            RockProgrammer Rock1 = new RockProgrammer();
            Console.ReadKey();
        }
    }
}

output:-

Why use Constructor:-

Constructor call automatically , If you want to create program in c# the program is call automatically,we don't need to call.

Example- Suppose that ,A website of Home Page automatically call ,by default call HOME page. We don't call . so this is the best example of constructor.   

Why does a constructor not return any value:-

Syntax :-   Public Rock()

CLR already define void method in Constructor . so we do not define void method in program . Constructor never be return. 

1 comment:

Anonymous said...

Good content rock programmer. Rock programmer is best study site.......

Post a Comment

| Designed by Rockprogrammer