Hello friend,
firstly
Quote:
Can I give my class a specific input
Definately you can, to give inputs to classes itself the concept of parametrized constructor was introduced.
Constructors are of 2 types:
Quote:
parameterless and parameterized
The default is called a parameterless constructor:
Now suppose you create a new class like this:
public class Class1
{
}
From the main method i can call it like this:
Class1 c = new Class1();
- Take notice: Are you passing any parameters inside the parenthesis of new Class1(). No you are not. It is "parameterless". Thats why i call it a parameterless constructor.
Now lets say according to some situation i want to initialize a method with a value when a class is initialized( At the start when the class is initialized) , then you can do this:
Class1 c1 = new Class1("Rahul");
and in Class1 i will say:
public Class1(string s)
{
string a = s;
Console.WriteLine(a);
}
Now summing up everything i can say:
In Class1:
public class Class1
{
public Class1()
{
}
public Class1(string s)
{
string a = s;
Console.WriteLine(a);
}
}
In the main i can say:
Class1 c = new Class1();
Class1 c1 = new Class1("Rahul");
These 2 above statements are called
Quote:
overloads of a class
Here there are just 2 overloads, you can specify many overloads.
hey friend just one homework: try putting static "keyword" before any constructor for example :
public static Class1(string s)
{
string a = s;
Console.WriteLine(a);
}
- Now compile the code and tell me what happens? and why?
good luck friend. I hope this has helped you. And i hope you have a plethora of doubts on this. And please ask me so that even i gain knowledge from your doubts.
Thanks and happy learning