Methods in C#

What is Method?

Method is a code block where the actual job is done & having programming logic. In other word the block of code which is created for a particular job that can be accessed from anywhere in the Program.

Method declaration:

    Public void mymethod ( ) //Access modifier return type method name (parameter list)
    {
       // method scope start.
       //Area for code
    } // method scope finish

Parameters:

Suppose we create method for squiring a number, for this we need a variable that take the number in it & further solve it. The variable that take number is defined in parameter list when method declares.

Arguments:

The values that pass from the parameter list is known as argument.

For Example:

  1. class Mathametics  
  2. {  
  3.    public int getproduct (int x,int y)  
  4.    {  
  5.       return x * y;  
  6.    }  
  7. }   
Now at run time we input 5 in num variable, this 5 is called argument.

Method Signature:

The compiler use the method signature to identify the methods found in a single class.

There are two definitions related to method signature, 
  • Microsoft definition
  • Internet definition

Now we will discuss both one by one.

Microsoft definition

The method signature is the combination of return type, method name & parameter list. The combination of these 3 must be different in a Program.

Internet definition

The combination of method name & parameter list called internet defined method signature.

I must go with Microsoft define method signature.

If we declare more than one method having same method signatures then the following error occurs,

error

Method Calling

After declaration of method in class the next step is to use it in the main scope of the program, for this we need first to create object in main body & the object created by “new” keyword.

    //class name object name= new class name( );
    Mathametics math = new Mathametics();

Then,

Object name. (All thepublic methods define in class show in Intel license window).

getproduct

Types of Method

There are four types of methods defined in C-Sharp language.

  • Take input ,Process, give output
  • Take input, Process, give no output
  • Take no input, Process, give output
  • Take no input, Process, give no output

Take input, Process, gives Output:

  1. public int getproduct (int x,int y)  
  2. {  
  3.    return x * y;  
  4. }  
Takes input, Process, gives no Output:
  1. public void getproduct(int a, int b, int c)  
  2. {  
  3.    Console.Write("The Product is {0}" + a * b * c);  
  4. }  
Takes no input, Process, gives Output:
  1. public int getdiv()  
  2. {  
  3.    int x = int.Parse(Console.ReadLine());  
  4.    int y = int.Parse(Console.ReadLine());  
  5.    return x / y;  
  6. }  
Takes no input, Process, gives no Output:
  1. public void getdiv()  
  2. {  
  3.   
  4. }  
Instance & non-instance Methods

In simple words the methods which are accessed without object is called non-instance methods, while the objects necessary for accessing the methods are called instance methods. 


Recommended Free Ebook
Similar Articles