Methods:

A method is used for performing actions (some programmers call methods as actions or operations or behaviors or functions or subroutines).

Syntax for declaring methods:

  access-specifier return-type Method-Name(data-type variableName1,data-type variableName2,...)
  {
      
  }
// return-type can be any c# supported inbuilt or user defined data type including void.

Writing a sample method

  public class Doctor
  {
   public string name="dr.Rajeev";
   public string qualification="MBBS";
   public string SuggestMedicine(string disease)
   {
     if(disease=="fever")
     {
       return "paracetmol";
     }
     else
       return "other suitable medicine";
   }
  }

Calling methods:

1. Use objectname [dot] method name for calling methods. 2. Pass required input to the method if the method is expecting any input. 3. Call method directly (without using object name) if calling method and called method are present in the same scope (class).

Sample code for calling methods:

   public class Doctor
   {
    public int age = 31;
    public string name = "Dr.Ravi";
    public string SuggestMedicine(string disease)
    {
      if(disease=="fever")
       return "paracetmol";
      else
       return "other suitable medicine";
    }
   }
   public class Test
   {
     public static void Main(string[] args) 
     {
        Doctor d=new Doctor();
        //call method by using objectname [dot] methodname.
        string medicine=d.SuggestMedicine("diarrhoea");
        /*SuggestMedicine method  takes a string as input; hence we are passing string 
         *data  in double quotations. Also, it is returning a string output and hence we 
         *have created a string variable for storing the output */
        Console.WriteLine(medicine);
     }
   }  
  
Note: In the above program the Main method acts as the entry point or the starting point of our application execution.

Types of methods:

1. Methods which take some input and give back some output. 2. Methods which take some input, but do not give any output back 3. Methods which give back some output, but do not take any input.
If a method does not return anything, then the return type of the method has to be specified as void.
types-of-methods
Notes:
1. Use Pascal casing for method names.
2. Use verbs as method names.
3. As per C# language rules, a method must be declared either in a class or in a struct (structure) or in an interface.

Types of variables

In C# language, variables are categorized into two types: 1. Local variables 2. Global variables or class level variables or fields.
local-global-variables

How methods are executed

By referring to the above sample code for calling methods, let's understand the mechanism of method execution using RAM architecture. When CLR observes the statement, Doctor d=new Doctor(); CLR always executes code from right to left and hence new Doctor() is executed first. This creates an object of Doctor class in heap memory. After this, it will then execute the left hand side code. This creates a variable d (local variable) in the stack. Next, when CLR observes the statement d.SuggestMedicine("diarrhoea"), it loads the SuggestMedicine method into the code segment and adds the method address into the object's storage area, as shown in the diagram. After completing the execution of the method code, CLR will then remove the method and its code from the code segment and also deletes all referenced local variables.
how-method-is-executed

Difference between local and global variables

Local variables are usually deleted faster than class level variables. Hence life time of local variables < life time of class level variables. In the above code sample, the disease variable is deleted immediately after execution of SuggestMedicine method, but the age and name variables are not deleted after completion of the method execution as they are global variables.

Calling different types of methods:

public class A
{
    public void M1()
    {

    }
    public int M2(int x, char y, bool b)
    {
        int r = 100;
        return r;
    }
    public string M3(int x, int y)
    {
        return (x + y).ToString();
    }
    public char M4()
    {
        return '$';
    }
    public void M5(float f1, char c, string s)
    {

    }
}
public class Test
{
    public static void Main(string[] args)
    {
        A a = new A();
        a.M1(); // Here there is no input or output.
        int r=a.M2(10, '^', false);
        /*Here we pass an int value, a char value and a boolean value. We get an int as 
         *output which we store into an int variable.*/
        string s=a.M3(10, 20);
        /*Here we pass 2 int values and get a string as output which we store into a 
         *string variable.*/
        char c=a.M4();
        /*Here there is no input to the method, but the output is a character which we 
         *store into a character variable.*/
        a.M5(10.78f, '8', "hello");
        /*Here we pass a float value, a char value and a string value; but do not declare
         *any variable since M5 does not return any data.*/
    }
}

Returning objects from methods

A method in C# can return a normal data type value like int, float etc.. But it can also return objects (since in C# language objects are considered as data types ).
Sample code for returning objects
public class Doctor
{
    public string name = "Dr.Rajeev";
    public int age = 36;
}
public class A
{
    public Doctor GetDoctorObj()
    {
        Doctor odoctor = new Doctor();
        return odoctor;
    }
}
public class Test
{
    public static void Main(string[] args)
    {
        A a = new A();
        Doctor od=a.GetDoctorObj();
        Console.WriteLine(od.name);
        Console.WriteLine(od.age);
    }
}

Memory architecture for the above program


Assignment on returning objects:

public class Nurse
{
    public string name = "veena";
    public int age = 38;
}
public class B
{
    public Nurse GetNurseObject()
    {
        //create Nurse class object and return the object 
    }
}
public class Test
{
    public static void Main(string[] args)
    {
        B b = new B();
        //Call GetNurseObject and print data present in nurse class object.

    }
}