Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




C# - METHOD


A Method in C# is a chunk of Code that is used to conduct a particular task. And that chunk is only executed when it is called.


Say if you want to add two numbers, you can have an add Method that would be dedicated to add the numbers.


Similarly, if you want to divide two numbers, you can have a divide Method that would divide the numbers.


So, instead of placing all the codes in a single place. You can distribute the work among different Methods. So, that your code looks more structured and clean.


How a Method in C# is created?


Below are the rules to create a Method :

  1. A C# method can be of any name(i.e. methodName mentioned above), followed by parentheses, i.e. (i.e. myMethod()).

  2. Inside the parentheses, you can specify any number of parameters (i.e. myMethod(int x, int y)).

  3. The block of code of the method should be inside the curly braces, i.e. {}.

  4. There is a return statement which is an optional statement and returns the calculated result to the caller(i.e. return <return_variable>).

  5. There is also a return type, which specifies the datatype of the returned value(i.e. return_type ).

  6. As said, a Method is a chunk of Code used to conduct a task. And the block of code should be inside starting brace { and an ending brace }.

Below is how an add method would look like:


int add(int firstNumber, int secondNumber)
{
	int result;
	result = firstNumber + secondNumber;
	return result;
}

And thats how we define a Method.


But it was mentioned in the Method description that a Method only executes only when it is called.


So, how a Method is called?


Let us see in the below example.


Note : Let us omit the argument and return type for now.

Example :



public class MyApplication
{
    public static void Main(string[] args)
    {
        myMethod();  
    } 
    
    static void myMethod() 
    {
		System.Console.WriteLine("This is my first method");
    }	
}


Output :



  This is my first method

So, in the above code, there are two parts,

  1. The Method definition.



    static void myMethod()
    {
    	System.Console.WriteLine("This is my first method");
    }


    void used in the above method definition tells us that no values needs to be returned.
    Note : We will explain the static keyword later.

  2. And the Method, call, inside the main() Method.



    myMethod();

So, in the above code we have defined a Method, myMethod(). You can give any name to the Method(In this case we have given the name myMethod()).


static void myMethod()
{
	System.Console.WriteLine("This is my first method");
}
C_Sharp


And the work of the Method, myMethod() is, just to print This is my first method.


Now, just remember one thing, the above Method, myMethod() will never execute until it is called.


And thus we have called the Method, myMethod() from Main() method.


public static void Main(string[] args)
{
	myMethod();
}

And the Method, myMethod() is called,


static void myMethod()
{
	System.Console.WriteLine("This is my first method");
}

We have placed static in the method definition because it is called from Main() method. And Main() method is static.


public static void Main(string[] args)

For now just remember that if any method is called from a static method. That should also be static.


Adding two numbers using Method and returning the result


Now, let us look at another example of adding two numbers and returning the result using a Method.


Example :



public class MyApplication
{
    public static void Main(string[] args)
    {
        int firstNum = 5;
        int secondNum = 8;
        int addedResult;

        addedResult = add(firstNum,secondNum);

        System.Console.WriteLine("The added value is : "+addedResult);
    }

    //Add method starts here.
    static int add(int firstNumber, int secondNumber) 
    {
        int result;
        result = firstNumber + secondNumber;
        return result;
    }
}


Output :



  The added value is : 13

Let us explain the above example with a practical scenario.


Say you are given a task of adding two numbers. And you would follow the below steps to achieve it :

  1. You would ask the firstNumber and secondNumber from the person who has given you this task.



    And the above C# Method expects the same thing.

    static int add(int firstNumber, int secondNumber)


    Here, firstNumber and secondNumber are the two numbers which a C# Method expects just like you.
    Note : firstNumber and secondNumber are called as arguments that we will be learning in a separate tutorial. And add() is the name of the Method.


    In addition to that, there is something called as the return type(i.e. Int). Which we will be explaining soon.
    C_Sharp

  2. The next thing you would do is, add the numbers and give the added value to the caller.



    Same thing is done by the Above Method.

    It adds firstNumber and secondNumber and stores the added value in a variable called result.

    result = firstNumber + secondNumber;


    It then returns the result to the caller(We will see next, who is a caller).

    return result;


    And since the returned value result is of type Int. We have specified Int in the returned type.
    C_Sharp

So far, we have seen how a Method works. Now, let us see who is the caller?


When the program execution begins,

C_Sharp

And we initialise the first variable firstNum with the value 5.


int firstNum = 5;
C_Sharp


Then in the next line, we have initialised the second variable secondNum with the value 4.


int secondNum = 8;
C_Sharp


Then we call the add(int firstNumber, int secondNumber) Method.


addedResult = add(firstNum,secondNum);

And C# searches for a Method with two arguments.


When it finds the Method, it assigns the value of firstNum to firstNumber and secondNum to secondNumber. And expects a value in return.

C_Sharp
C_Sharp
C_Sharp

And gets into the block of the int add(int firstNumber, int secondNumber) Method.


This is where the numbers are added,


result = firstNumber + secondNumber;

And the result is stored in a variable result.

C_Sharp

And in the next line we return the added value(i.e. result).


return result;

And the returned value goes back to the line, where the Method was called.


addedResult = add(firstNum,secondNum);

And the variable value gets the added result(i.e. 13) from the variable result.

C_Sharp

And in the next line the added value is printed as output.


System.Console.WriteLine("The added value is : "+addedResult);