Explain constructor.

Answer Posted / vinay tiwari

//Constructor is a special member function its name is same
//as class name.It is used to allocate memory to object and
initilizes
//its field .you can also say that it is used to initilized
the object
//constructor invoke automatically when the object of its
class is created
class a
{
public int i;
public a()//default constructor
{
i=10;
}
public a(int x)//parameterized constructor
{
i=x;
}
public a(a ob)//copy constructor
{
i=ob.i;
}
public void display()
{
System.Console.WriteLine(i);
}
}
class b
{
static void Main(string [] args)
{
a ob=new a();
ob.display();
a ob1=new a(77);
ob1.display();
a ob2=new a(ob);
ob2.display();
}
}

Is This Answer Correct ?    10 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

what class is underneath the sortedlist class?

715


What is the purpose of a namespace?

659


Overloaded constructor will call default constructor internally?

791


What's different between c# and c/c++?

707


What is string literal in c#?

685


Why we use get and set property in c#?

693


Why do we use delegates in c#?

677


Why do we need constructor in c#?

687


Explain publishers and subscribers in events.

716


what is a enumeration in c#

775


What is a c# delegate?

791


What is short in c#?

736


Which of the following API is used to hide a window?

765


What is the default value of boolean variable?

657


What is uint c#?

661