Using Methods in C#

Before coming to methods and there types first we shall know what r the features of Methods....

  • Method is a set of one or more statements or instructions.
  • Methods r mainly used for data hiding (i.e) providing the facility of encapsulation and also abstraction.
  • They allow the user to implement the concept of reusability .
  • They play a key role in modular programming ,when a application is divided into methods then it ill be easy to maintain the code and also to debug.
  • Methods r used for performing repetitive tasks such as fetching specific records and text. They allow user to break an application into discrete logical units, which makes the application more readable.
  • We can reuse the code written in a method and can be executed any number of times by calling methods with little change or else no change .
To use methods, you need to perform 2 things mainly
  1. Define method.
  2. Call method.
Defining methods:

defining a method means declaring the elements of its structure. methods r defined with the following syntax......
(para list)
{
//................Method body;
}

  • Access specifier:
    This explains the extent to which a variable and mtd can be accessed.
  • Return type:
    A method can return any type of value, if method does not return any value then use void as the return type.
  • Method name:
    This plays a prominent role in shaping a class. Method names can not be keywords of the programming language and it is a good practice to follow follow pascal case (eg: Methodone). It can not be same as a variable name also .
  • Parameter list:
    This is used to receive and pass the data from one method to other. We can leave the parameter list empty with out any values.
  • Method body:
    Method body mainly contains the code which is required to perform some specific operation.
example:
class Calculator
{
public int AddNumber(int n1, int n2)
{
int res;
res=n1+n2;
return res;
}
}
Types of Methods
  • calling Methods
    After defining a method , you can use it by its name. The method name is followed by parentheses even if the method call has no parameters.
example:
using System;

class Calculator
{
public int AddNumber(int n1, int n2)
{
int res;
res=n1+n2;
return res;
}
static void Main(string [] arg)
{
Calculator C=new Calculator();
int total=C.AddNumber(10,20);
Console.WriteLine("Te result is :{0}",total);
Console.ReadLine();
}
}
Execution process:
  • In the above program the execution starts from main function . so initially memory is allocated dynamically for the Calculator class and a object is created.
  • Nw by using the object we r trying to access the code defined inside the method. When the method (AddNumber) is accessed the actual arguments(10, 20) supplied in the method is passed to the formal parameters(int n1,int n2) and these values r stored in n1 and n2.
  • since the method uses a return type as int, it is the duty of the user to return a value of type int . so after performing the addition operation the data is returned and the returned value is caught by a variable (total) which is in main function.
  • Last but not the least.............. The value is printed.
  • Here We have to remember one thing that return type of the main method is 'void ' so it doesnt return any value.
example:
/*.................. Recursion method...............*/
using System;

class Number
{
public int Factorial(int n)
{
int res;
if(n==1)
return 1;
else
{
res=factorial(n-1)*n;
return res;
}

}
static void Main(string [] arg)
{
Number obj=new Number();
Console.WriteLine("Factorial of 3 is "+obj.factorial(3));
Console.WriteLine("Factorial of 4 is "+obj.factorial(4));
Console.WriteLine("Factorial of 5 is "+obj.factorial(5));
Console.ReadLine();
}
}

In above program, the factorial() method is recursive method. If the value entered by the used is not 1, this method will call itself.

0 Comments:

Post a Comment



Newer Post Older Post Home