Monday 12 January 2015

Method in C-sharp

Advantage:

1.Method also known as function. these terms use interchangeably.
2.In method you define your logic at once and use it at many places.
3.It makes maintenance of your application easy.

Syntax:

[attributes] access-modifiers return-type method-name(Parameter)
{
      method body;
}

a. Return type can be any data type or void.
b. Method name can be a meaningful name.
c. Parameters are optional.

Static and Instance methods

a. When a method declaration includes a static modifiers, that method is said to be static method.

b.Static method is invoked using class name, where as an instance method is invoked  using instance of the class.

  1. class Demo  
  2. public static void Main()           //main method  
  3. {  
  4.     Demo.Add();                        //static method call  
  5.     Demo d = new Demo();  
  6.     d.sub();                                   // instance method call  
  7. }  
  8.   
  9. public static void Add()                  //Static method  
  10. {  
  11.     
  12. }  
  13.   
  14. public void Sub()                       //Instance method  
  15. {  
  16.   



c. Multiple Instances of class is created (or instantiated) and each instance has its own separate method. However when a method is static, there are no instance of the method, and you can invoke only the one definition of static method.





Types of Method

1.Pass by value/value parameter
2.pass by reference/reference parameter
3.Parameter array
4.Out Parameter



1.Pass by Value/Value Parameter



I and J are pointing to different memory location operation one variable not affect the value of other variable.






Example:

  1. class Demo  
  2. {  
  3.    public static void Main()  
  4.    {  
  5.        int i = 0;  
  6.        Simplemethod(i);  
  7.       Console.WriteLine(i);  
  8.    }  
  9.   
  10.   public static void Simplemethod(int j)  
  11.  {  
  12.       j=101;  
  13.  }  
  14. }

Output: 0


Henceforth, Pass by value parameter is creates a copy of the parameter passed, so modification does not affect each other.



2.Pass by reference/Reference Parameter

I and J pointing to the same memory location.  Operation on one variable will directly affect the  value of other variable.






Example:

  1. class Demo    
  2. {    
  3.    public static void Main()    
  4.    {    
  5.        int i = 0;    
  6.        Simplemethod(ref i);    
  7.       Console.WriteLine(i);    
  8.    }    
  9.     
  10.   public static void Simplemethod(ref int j)    
  11.  {    
  12.       j=101;    
  13.  }    

Output: 101


The ref method parameter keyword on a method parameter causes a method to refers to the same variable that was passed into the method. Any change made to the parameter in the method will be reflected in that variable when control passes back to the calling method.



3.Out Parameter:

               Use when you want a method returns more than one value.

Example:

  1. class Demo  
  2. {  
  3.    public static void Main()  
  4.    {  
  5.         int total=0;  
  6.         int product = 0;  
  7.       
  8.        calculate(10,20,out total,out product);  
  9.   
  10.        Console.WriteLine("Sum = {0} && Product = {1}", total, product);  
  11.    }  
  12.   
  13.   public static void Calculated(int FN, int SN, out int Sum, out int Product)  
  14.   {  
  15.        Sum=SN+FN;  
  16.        Product = SN*FN;  
  17.   }  
  18. }  

Screenshot of output
Fig:Output of out Parameter

   


4.Parameter Arrays:

             The PARAMS KEYWORD LETS YOU SPECIFY A METHOD THAT TAKES A VARIABLE NUMBER ARGUMENTS. YOU CAN SEND A COMMA SEPARATED LIST OF ARGUMENTS  OR AN ARRAY OR NO ARGUMENT.
            Params keyword should be last one in a method declaration and only one params keyword is permitted in a method declaration.

Example:

  1. class System;  
  2. {  
  3.    public static void Main()  
  4.    {  
  5.        int[] Numbers = new int[3];  
  6.               
  7.          Number[0]=101;  
  8.          Number[1]=102;  
  9.          Number[2]=103;  
  10.   
  11.        // ParamsMethod();  
  12.         ParamsMethod(Numbers);  
  13.        // ParamsMethod(1,2,3,4,5);  
  14.    }  
  15.   
  16.   public static void ParamsMethod(params int[] Numbers)   // method Parameter
  17.   {  
  18.      Console.WriteLine("There are {0} Elements ", Numbers.Length);  
  19.   
  20.      foreach(int i in Numbers)  
  21.      {  
  22.           Console.WriteLine(i);  
  23.      }  
  24.   }  
  25. }  

Screenshot of output
Fig: Output of Params Method