Constructors and Methods are the heart for our programming languages like java,c#.net,vb.net etc Any developer first know the value of constructors and methods and what are the difference between method and constructors.How it can be placed in programming development.
Methods in C#.Net
methods |
1.Parameterless method
2.Parameterize method
1.Parameterless method:-
While defining a method if we didn't declare any parameters which is called as parameterless method.Q)When we will go for parameterless method?
Ans:According to method functionality if the method doesnot required any value then we will go for parameterless method.EX:-class Program
{
void Display()
{
Console.WriteLine("welcome to c#.net");
Console.WriteLine("welcome to Asp.net" );
}
}
2.Parameterize method:-
While defining a method if we have declared parameter which is called as parameterized method.Q)When we will go for parameterized method?
Ans:According to requirement whenever a method is required some value for it's functionality then we will go for parameterized method.EX:-class Program
{
void add(int a,int b)
{
int c=a+b;
Console.WriteLine("result is:"+c);
}
}
According to method return types methods are classified in to two types
1.Void method
2.Non-Void method
1.Void method:-
A method which is not returning any value according to it's functionality is called as voidmethod.Void method return type should be void.
Q)When we will go for void method?
Ans:According to the requirement if the method does not require to return any value we will go for void method.2.Non-Void method:-
A method which is returning some value is called as non-void method.Q)When we will go for non-void method?
Ans:According to method functionality if it requires to return some value we will go for non-void method.the return type of method will be depending on type of the value which is returning by the method.If the method requires to return numerical values then method return type can be any one of the numerical data type like int,long,short............................
Ex:-int fun(int a)
{
return a;
}
char fun1(char a)
{
return a;
}
Again methods are classified into two types
1.Instance method or Non-static method
2.Static method
1.Instance method or Non-static method:-
While defining a method if we didn't use static keyword which is called as instance method.If we want to access instance method with the help of object.Ex:-class Employee
{
ulong empid;
string empname;//instance variables
internal void Display()
{
Console.WriteLine("Enter employee id ");
ulong empid=ulong.Parse(Console.ReadLine());
Console.WriteLine("Enter employee name ");
string empname=Console.ReadLine();
Console.WriteLine("employee id is:"+empid);
Console.WriteLine("employee name is:"+empname);
}
}
class Program
{
static void Main(string args[])
{
Employee a=new Employee();
a.Display();//calling instance method
Console.ReadLine();
}
}
2.Static method:-
While defining a method if we are using static keyword which is called as static method.If we want to access static method we have to access with the help of class name.Ex:-
class Employee
{
static string compname;
static string comploc;//static variables
static internal void Display()
{
compname="wipro";
comploc="Hyderabad";
}
}
class Program
{
static void Main(string args[])
{
Employee.Display();//calling static method
Console.ReadLine();
}
}
Methods topic in c#.net are most important to everyone so please share it to your friends also through facebook,twitter,google plus.
0 comments:
Post a Comment