C# Methods

C# Methods are the group of statements that perform a task altogether.

Storing a set of statements in a single module eliminates the need to rewrite the same code or makes a specific operation more frequently used. The method in C# is a discrete code block that contains many statements that perform specialised tasks. To learn more about the various C Sharp approaches, keep scrolling down.

C# Methods are basically the block of codes or statements in a program that gives the user the ability to reuse the same code. It helps in saving the excessive use of memory. To use a method, you first have to define the method and then calling that method.

C# Methods are often useful for making code more reusable by eliminating duplication. If the same functionality is available in other regions, we can create a method with the necessary features to use it wherever the application calls for it.

C# Methods Declaration:

The way to construct method including its naming is known as method declaration.

image 8
Syntax:
C Methods

C# Methods consists of the following components

Declaration of method in C# consists of the following components as given below:
ComponentsDescription
ModifierDefines the access type from where the method is being accessed.
Methods in C# can be Public, Protected, Private access modifiers.
Return typeDefines the data type returned by the method.
Parameter listA comma-separated list of the input parameters is defined in the parameter list.
It preceded with their data type, enclosed in parenthesis.
Body of the MethodUsed to refers to the task’s line of code to be
performed by the method during its execution.
Name of the MethodUsed to describe the name of the user-defined method
by which the user calls it or refers to it.

Method Calling:

Calling Methods in C# is done when the user wants to execute the method. First we have to call the method to use its functionality.

Method Parameters:

There is a set of parameters that are defined by Methods in C#. A single method can take any number of parameters. Each parameter needs a type and a name which is included in the method definition. They can be either int, char, long or float, or double. We have to pass the parameters to the method when we call parameters.

To pass parameters to a method, there are 3 ways as given below:

  • Passing Parameters by Value
  • Passing Parameters by Output
  • Passing Parameters by Reference

1- Passing Parameters by Value:

This method is considered the default mechanism for passing parameters to a method. In this method, it copies the actual value of an argument into the formal parameter of the function. In this method, when we made changes to the parameter inside the function, it has no effect on the argument.

using System;

namespace CalculatorApplication {
  public  class NumberManipulator {
      public void swap(int A, int a) {
         int Grade;
         
        Grade = A; 
         A = a;    
         a = Grade; 
      }
   public static void Main(string[] args) {
         NumberManipulator n = new NumberManipulator();
               
         int A = 80;
         int a = 95;
         
         Console.WriteLine("Before swap, value of A : {0}", A);
         Console.WriteLine("Before swap, value of a : {0}", a);
        
         n.swap(A, a);
         
         Console.WriteLine("After swap, value of A : {0}", a);
         Console.WriteLine("After swap, value of a : {0}", A);
         
         Console.ReadLine();
      }
   }
}

2- Passing Parameters by Output:

The output method helps in returning more than one value in the C# methods. A return statement can be used for returning only one value from a function. But using output parameters, we can return two values from a function.

using System;

namespace CalculatorApplication {
  public class NumberManipulator {
      public void getValue(out int a ) {
         int ID = 1;
         a =ID;
      }
     public static void Main(string[] args) {
         NumberManipulator n = new NumberManipulator();
  
         int x = 5;
         
         Console.WriteLine("Value of a before method call : {0}", x);
      
         n.getValue(out x);

         Console.WriteLine("Value of a after method call : {0}", x);
         Console.ReadLine();
      }
   }
}

3- Passing Parameters by Reference:

A reference parameter is a reference to a memory location of a variable. This method copies the reference to the memory location of an argument into the formal parameter. In this case, changes made to the parameter affect the argument. No new storage location is created for these parameters. It represents the same memory location as the actual parameters. To declare the reference parameters we use the “ref” keyword.

using System;

namespace CalculatorApplication {
  public  class NumberManipulator {
       public void swap(ref int A, ref int a) {
         int Grade;
         
        Grade = A; 
         A = a;    
         a = Grade; 
      }
   public static void Main(string[] args) {
         NumberManipulator n = new NumberManipulator();
               
         int A = 80;
         int a = 95;
         
         Console.WriteLine("Value of A before swap : {0}", A);
         Console.WriteLine("Value of a before swap : {0}", a);
        
     n.swap(ref A, ref a);

         
         Console.WriteLine("Value of A after swap : {0}", A);
         Console.WriteLine("Value of a after swap : {0}", a);
         
         Console.ReadLine();
      }
   }
}