- Constructors are similar to methods.
- They always take the name of the class but no return type.
- Constructors cannot be called/ inherited can only be invoked by 'new' operator.
- They allocate memory to all of class/member variables to keep default values in it.
- Class has one single constructor & it is private and can never be instantiated.
- Static Constructors -- Used for initializing only static members of a class..These are invoked very first time class is loaded in memory. No args. No Access Modifiers.
- Non-Static Constructors -- Used for initializing static & non-static methods. Invoked every time a new object is created.
Example on Constructor:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
class add
{
public add(int a, int b)
{
int totsum;
totsum = a + b;
MessageBox.Show(totsum.ToString());
}
}
private void button1_Click(object sender, EventArgs e)
{
add emmu = new add(5, 3);
}
}
0 comments:
Post a Comment