Hello,
what i can see is that you are confused between a constructor and a class. Lets start with some basics:
Quote:
What is a Class?
Class is a collection of related things(it should be). Classes are used to organize the functionality of your code.
For example you have a class which contains all the methods related to networking: For example, pinging, creating a socket etc.
When i create a class i am just creating its blueprint. How should i realize it? to do that i will say:
Class1 c = new Class1();
Please note: the
Quote:
new
keyword is used to allocate memory for the
Quote:
object
of the class.
Pay attention to the words i have put in quotes. I can use the functionality of that class using its object. So to use the functionality of Class1, i can now use c.
So i say c.some_method();
Quote:
What is a constructor?
Constructor can be called a special method which is used to initialize the fields of a class.Its name is the same as that of the class. Constructors are of 2 types: Parametrized and Parameter less.
As explained by my friend(Praveenpk3) in Solution 3 there are many other types of constructors, referring to his solution,
Quote:
Different Constructor types are as :
Default Constructor
Parameterized constructor
Private Constructor
Static Constructor
Copy Constructor
Look at the code below:
public class Class2
{
string s;
public Class2() {
}
public Class2(string s) {
this.s = s;
Console.WriteLine(this.s);
}
}
Quote:
Why to use a constructor?
- Suppose that you want to initialize some field of a class at start up? how do you do that?
using the code i showed you.Experience will help you figure out when to use a parameterized constructor.
Quote:
What is a method?
A method is some thing which performs some operation or function.
It can take one/many inputs perform an action
Quote:
depending on those inputs
and return results.
For example :
public bool method1(int i)
{
return i == 1;
}
I dont have a void here. Why? because it does not return a void(which means nothing/empty). It returns something, a result, whose type is a bool.
It may not take any inputs and simply return the results.
public bool method1()
{
return false;
}
It may neither take any input nor return any results.
public void method2()
{
Console.WriteLine("hello");
}
I hope this has helped you to learn the significance of a return type.
Quote:
Homework3:
public bool method1(int i)
{
return i == 1; ///returns true of i =1
}
public bool method1()
{
return false;
}
What is the above phenomena called? Why to use it?
Start studying and happy learning.
One last thing: Please do the homework, will be good for you.
Thanks