• Have Any Question?
  • contact@eitworld.com

csharp methods





C# Methods

 

  • The code that is designed to perform some specific task is known as Method.
  • A method is a basic part of the program. It can solve a certain problem, eventually take parameter and return result.
  • Every C# program contains a method necessarily that is called Main method which is the entry point of the program.
  • Basic structure of method contains the two parts:
    • Declaring Methods
    • Invoking Methods

Let us begin with the declaration of method:

 

Declaring Methods

 

  • Method can be declared by specifying the access level, type of return value, the name of method and method parameters if any.
  • Method parameters are surrounded by the parenthesis and are separated by commas.
  • Empty parenthesis shows that method does not contain any parameter.
  • The basic syntax to declare a method is given below:
access-specifier return-type method-name (parameter list)
{
Method body
}

access-specifier:It determines the visibility of the methods from the other class or structure. It can be public, private, protected or static. We will study these access specifiers in detail in further section.return-type:A method can return a value of any data type that is known as return type. If method does not return any value then its return type will be ‘void’.method-name:A method name should be meaningful and it cannot be same as the name of any variable or constant.parameter list:The parameter list is a list with zero or more declarations of variables, separated by a comma, so that they will be used for the implementation of the method’s logic. Method body:It contains block of statements which performs a specific task.

 

Invoking Methods

 

    • Once a method is defined, it must be activated for performing the task. The process of activating a method is known as invocation or calling of method.
    • A method can be divided into two parts: a) Static method b)Non static method
    • Static methods can be called with the name of class and method name as given below:
Classname.method();

    • Non-static method is called with the instance of class and method name as given below:
Objectname.method();

Invoking a static method

 

  • Static methods can be called with the name of class.
  • Here is an Example of calling a static method:
Example

 

using System;
namespace method

Example

{
class staticmethod
{
static void printhello()//definition of static method
{
Console.WriteLine(“nHello”);
}

static void Main(string[] args)
{

staticmethod.printhello();//calling of static method with the name of class
Console.ReadLine();
}
}
}

In the above program when the static method printhello() is called with the name of class the Control of program jumps on the definition of static method present in the static method class and then executes the statements present in the method printhello. After execution of method the control comes back to the next statement of calling statement present in main method that is ‘Console.ReadLine()’. The

Output

of the above program will be:

 

Output

 

Hello

Invoking a Non-static method

 

  • Non-static method is called with the instance of class.
  • Here is an
    Example

    of non-static method:

Example

 

namespace methodExample
{
class nonstaticmethod
{
public void printhello()//definition of nonstatic method
{
Console.WriteLine(“nHello”);

}

static void Main(string[] args)
{
nonstaticmethod s1=new nonstaticmethod();//instance of class
s1.printhello();//calling of nonstatic method with the name of class
Console.ReadLine();
}
}
}

The Output of the above program is as follows:

 

Output

 

Hello

Passing parameters to a method

 

  • When you have to call a method with the parameters then you have to pass the parameter to a method.
  • There are two ways of passing parameter to a method:
    • Pass by Value
    • Pass by reference
Pass by Value

 

  • To pass a parameter by value, you simply write the name of parameter.
  • When you pass a parameter by value, the method cannot change the value of the variable.
  • When user pass a variable by a value, just a copy of the variable specified in calling method is passed to the called method,not the actual variable.

Here is an Example of parameter passing by value:

 

Example

 

using System;
namespace methodExample
{
class passbyvalue
{
static void valueparameter(int a,int b)
{
a = a + 1;
b = b + 1;
Console.WriteLine(“values of a and b in called function:na={0}nb={1}”, a, b);
}
static void main()
{
int a = 5, b = 6;
Console.WriteLine(“values of a and b before passing parameter by value:na={0}nb={1}”, a, b);
passbyvalue.valueparameter(a, b);//Parameter Pass by value.
Console.WriteLine(“values of a and b does not change passing the parameters by value”);
Console.WriteLine(“a={0}nb={1}”, a, b);
Console.ReadLine();
}
}
}

The Output of the above program will be as follows:

 

Output

 

values of a and b before passing parameter by value:
a=5
b=6
values of a and b in called function:
a=6
b=7
values of a and b does not change passing the parameter by value:
a=5
b=6

Pass by reference

 

  • To pass the parameter by reference you have to use the ‘ref’ keyword before the parameter’s name.
  • When user pass the parameter by reference the memory address of the variable passed to the method. Thus the method can modify the value of the variable passed by reference.
  • Here is an
    Example

    of Parameter pass by reference:

Example

 

using System;
namespace methodExample
{
class passbyreference
{
static void referenceparameter(ref int a, ref int b)
{
a = a + 1;
b = b + 1;
Console.WriteLine(“values of a and b in called function:na={0}nb={1}”, a, b);
}
static void Main()
{
int a = 5, b = 6;
Console.WriteLine(“values of a and b before passing parameter by reference:na={0}nb={1}”, a, b);
passbyreference.referenceparameter(ref a, ref b);//Pass by reference.
Console.WriteLine(“values of a and b modify after passing the parameters by reference:”);
Console.WriteLine(“a={0}nb={1}”, a, b);
Console.ReadLine();
}
}
}

The Output of the above program would be as follows:

 

Output

 

values of a and b before passing parameter by reference:
a=5
b=6
values of a and b in called function:
a=6
b=7
values of a and b modify after passing the parameter by reference:
a=6
b=7

Return Statement

 

  • Return statement returns the value to the calling method.
  • Return statement is a jump statement that terminates the execution of a method and returns the control back to calling method.
  • Return statement cannot use if we declare the return type of the method as void. You have to declare the return type other than void (int,float etc.) for returning the value from the function.
  • Here is an Example of Return statement which returns integer value to calling method:
Example

 

using System;
namespace methodExample
{
class returnstatement
{
static int add(int x, int y)
{
int z;
z = x + y;
return z;//return the addidtion of parameters to calling function
}
static void Main()
{
int a, b, c;
a = 2;
b = 3;
c = returnstatement.add(a, b);//calling add function and store the result in variable c
Console.WriteLine(“Addition of a={0} and b={1} is:c={2}”, a, b, c);
Console.ReadLine();
}
}
}

The Output the above program would be as follows:

 

Output

 

Addition of a=2 and b=3 is:c=5

Recursion

 

  • A method can also call itself. So the process in which a method calls itself is known as recursion.
  • Here is an Example of find factorial of a number by using recursion:
Example

 

using System;
namespace methodExample
{
class recursion
{
static int factorial(int num)
{
int fact = 1;
if (num == 1)
{
return 1;
}
else
{
fact = num * factorial(num – 1);//recursion
return fact;
}
}
static void Main()
{
int num,fact;
Console.WriteLine(“Enter the number to find its factorial:”);
num = int.Parse(Console.ReadLine());
fact = recursion.factorial(num);
Console.WriteLine(“Factorial of number {0} is:{1}”, num, fact);
Console.ReadLine();
}
}
}

The Output of the program would be as follows:

 

Output

 

Enter the number to find its factorial:
5 Factorial of number 5 is:120



Real Time Web Analytics