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 is a dynamic assembly?

529


What happens if a static constructor throws an exception?

496


how can one use hcl and c sharp together?

1593


How do I trim a space in c#?

446


What is difference between static and readonly in c#?

454






What is sorting in c#?

521


What is unrecognized escape sequence in c#?

511


What are extensions in c#?

540


What is an escape sequence?

516


Is static class thread safe in c#?

475


What are the basic string operations? Explain.

514


Can the nested class access, the containing class. Give an example?

477


Can you explain template pattern?

572


What is hashmap in c#?

480


What is form feed in c#?

573