Pages

Friday, May 10, 2013

Methods in C#

A method is a code block containing a series of statements. Methods must be declared within a class or a structure. It is a good programming practice that methods do only one specific task. Methods bring modularity to programs. Proper use of methods bring the following advantages:
  • Reducing duplication of code
  • Decomposing complex problems into simpler pieces
  • Improving clarity of the code
  • Reuse of code
  • Information hiding

method signature :

A method signature is a unique identification of a method for the C# compiler. The signature consists of a method name, and the type and kind (value, reference, or output) of each of its formal parameters. Method signature does not include the return type.

Types of methods:
Some methods are there which will take multiple inputs and give one output.
Some methods which will take multiple inputs and give no output
Some methods which won't take any input and give output.
some methods which won't take any input and give no output.
 
example:
public class method
 
{
 
     public int M1(int x,string y) //public is access modifier,int is return type,M is method       
 
      {
          int r=10;       //This method is taking 2 parameters
          return r;
         }
 
      public char M2()         
       {
          char c1='A';    //This method is not taking any parameters
          return c1;
 
       }
 
     public void M3(char c1,bool b1,string s1,short s)
        {
 
            ................
 
           ...........       //This method is taking 4 parameters
 
           ...........
        } 
 
}
 
   
 

No comments:

Post a Comment