find and solve || findandsolve.com
Please wait.....

C# methods Examples

In the c# a method group of code statements which only runs when it is called.In the tutorial we will explains what are functions in C# programming with different simple Examples.

We can pass data, known as parameters, into a method.

To use a method

  • Define the method
  • Call the method

Defining Methods in C#

When we define a method, basically declare the elements of its structure like as given below.

<AccessSpecifier> <ReturnType> <MethodName>(ParameterList) {   
}

Explain the various elements of a method

  • AccessSpecifier: Access Specifier determines the visibility of a variable or a method from another class. For Example, in C# there are Public, Protected, Private access modifiers. 
  • ReturnType:A method may return a value or not depend on function. The return type is the data type of the value the method returns. If the method is not returning any values, then the return type is void.         
  • MethodName: To make a method name is always unique identifier and it is case sensitive. It cannot be same as any other identifier declared in the class.   
  • ParameterList: Enclosed between parentheses, different types of parameters are used to pass and receive data from a  created method. Parameters are optional; that is, a method may contain no parameters.

Create a Method

A method is defined with the name of the method, followed by parentheses (). In the C# programming language provides some pre-defined methods, which you already are familiar with, such as Main(), but we can also create our own methods to perform certain actions:

                                                  In the following code example shows a function secondNumber that takes two integer values and returns the sum of the two number. It has public access specifier, so it can be accessed from outside the class using an instance of the class.

public  class MethodExample{
  public int SumTwoNumber(int firstNumber,int secondNumber) {
      /* local variable declaration */
      int result;
      result = firstNumber+secondNumber;
      return result;
  }
}

Calling Methods in C#

To call (execute) a method, write the method's name followed by two parentheses () and a semicolon ";" In the following example, SumTwoNumber() is used to print a text (the action), when it is called:

using System;
public class Program
{  public class MethodExample{
  public int SumTwoNumber(int firstNumber,int secondNumber) {
      /* local variable declaration */
      int result;
      result = firstNumber+secondNumber;
      return result;
  }
}
    // Main Method
    public static void Main(string[] args)
    {
      int a =20;
    int b=30;
    int sum;
    //callling SumTwoNumber function like as given below
    MethodExample obj= new MethodExample();//create MethodExample class object
  sum = obj.SumTwoNumber(a,b);//call method
  Console.WriteLine("The Sum of Two Number is : {0}",sum);
    }
}

Output

The Sum of Two Number is : 50