Explain constructor.

Answer Posted / k.gnaneshwar

*-> Constructor is used for initializing the variables
during object creation.
*-> that means the code required for initializing the
variable should be writen in constructor.
CONSTRUCTOR DECLARATION RULES
* Constructor look like a method.
* Constructor have no return type.
* Constructor name should be same name as the class name.

(prog)

class simpleAddition
{
int a,b; //variable declaration
simpleAddition() //constructor declaration
{
a=6;
b=3;
}
int add() // method declaration
{
return a+b;
}
}

output:- 9

Is This Answer Correct ?    2 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How many constructors can a class have c#?

474


What's c# ?

594


What is the usage of Enumeration in C# Programming and is it good to use or not ?

561


Can a static class contain non static members?

556


How many digits are in an integer?

478






When should I throw an exception?

557


What is difference between ienumerable and list?

518


Can we make a class private in c#?

504


What is a decimal in c#?

504


What is equal c#?

510


How many bytes is an int in c#?

507


What are primitive data types in c#?

525


What is difference between the "throw" and "throw ex" in .net?

488


Write the syntax for catching an exception in c#?

491


What are delegate methods?

470