18-12-2010
Object: Object is a instance of a class, whenver we want to access the class members we can access by using concerned class object.
Syntax to create an object:
<Class name> <object name> = new <class name()>;
Eg: For Object creation:
myclass mc = new myclass();
Syntax to access a class member:
<object name>.<class member name>;
Eg: For a class & object & method:
Method: Method is a member of a class, which is definig the action of the object.
Syntax to define a method:
<access modifier> <return type> <method name()>
{
// Body of the method
}
Eg: Prog:
namespace class myclass
{
public class myclass
{
//declaring a variable
public int i=20;
//defining method
public void display()
{
Console.WriteLine("Good Evening");
}
}
Class program
{
Void main()
{
//creating object
myclass mc = new myclass();
Console.writeLine("i value is " +mc.i);
mc.display();
Console.ReadLine();
}
}
}
Output: i value is 20
Good Evening
According return types or returnig the value methods are two types.
1. Void method
2. Non void method
1. Void method: The method which is not returning any value that method can be called as void method.
Void method return type we should declare as void.
For Eg:
Public void display()
{
Console.WriteLine("Good Evening");
}
2. Non void method:
The method that returnig a value is called as non void method.
For non void methods we should declare the return type as the data type of the value which is returnig by method.
Eg: For non void method:
Public int display()
{
return 111;
}
Eg: For void method & non void method:
namespace method example
{
Public void greetings()
{
Console.WriteLine("Happy new year");
}
Public int add(int a, int b)
{
return a+b;
}
}
Class Program
{
Void main()
{
myclass mc = new myclass();
mc.greetings();
int x = mc.add(10,5);
Console.WriteaLine("addition result is " +x);
Console.ReadLine();
}
}
}
Output: Happy new Year
Addition result is 15