Methods and Anonymous methods

C# Methods are a set of statements or code blocks to perform a specific operations and may return a value.
These are defined once and are reusable as many times as needed.

Syntax:

    ( )
    {
        statements..
    }

 

These are classified into 2 types:

- Static Methods

- Non Static Methods (Instance Methods)

Static Methods:

Methods having static keywords are Static Methods. These methods are accessed with
the class name.

Example:

 

using System;
namespace SampleProgram {
  class MySample {
    public static int GetSquare(int r) // static method
    {
      return r * r;
    }
    public static void Main(string[] args) {
      Console.WriteLine("The Square value is " + MySample.GetSquare(4));
    }
  }
}

 

The output will be:

The Square value is 16

 

Non Static Methods (Instance Methods):

Methods having no static keyword are Instance Methods. These methods are accessed by the object of
the class.

Example:

 

using System;
namespace SampleProgram {
  class MySample {
    public int GetSquare(int r) //non static method
    {
      return r * r;
    }
    public static void Main(string[] args) {
      MySample obj = new MySample();
      Console.WriteLine("The Square value is " + obj.GetSquare(4));
    }
  }
}

 

The output will be:

The Square value is 16

 

Method Parameters:

A Method can have 4 different types of parameters, as listed are:

-  Value Parameters

-  Reference Parameters

-  Output Parameters

-  Parameter arrays

Value Parameters:

Value type variable stores a copy of the value.

Example:

The Swap does not happen as the value type store the copy of the value.

 

using System;
namespace SampleProgram {
  class MySample {
    public static void GetSwapInfo(int a, int b) {
      int t = a;
      a = b;
      b = t;
    }
    public static void Main(string[] args) {
      int p = 10,
      q = 20;
      MySample.GetSwapInfo(p, q);
      Console.WriteLine($ "The Swap Data: {p},{q}");
    }
  }
}

 

The output will be:

The Swap Data: 10,20

 

Ref Parameters:

Ref type variable stores the address of the value.

Example:

The Swap happens as the ref type store the address of the value.

 

using System;
namespace SampleProgram {
  class MySample {
    public static void GetSwapInfo(ref int a, ref int b) {
      int t = a;
      a = b;
      b = t;
    }
    public static void Main(string[] args) {
      int p = 10,
      q = 20;
      //passing reference parameter will swap the data.
      MySample.GetSwapInfo(ref p, ref q);
      Console.WriteLine($ "The Swap Data: {p},{q}");
    }
  }
}

 

The output will be:

The Swap Data: 20,10

 

Output Parameters:

Output parameters are mainly used in methods to produce multiple returns
values.

Example 3:

 

using System;
namespace SampleProgram {
  class MySample {
    public static void GetInfo(int a, out int b) {
      b = 50;
    }
    public static void Main(string[] args) {
      int p = 10;
      MySample.GetSwapInfo(p, out q);
      Console.WriteLine($ "The Data: {p},{q}");
    }
  }
}

 

The output will be:

The Swap Data: 10,50

 

Parameter Arrays:

Pass an array as multiple values to a method parameter.

Example:

 

using System;
namespace SampleProgram {
  class MySample {
    public static void PrintAgeInfo(int[] arr) {
      foreach(int i in arr) {
        Console.WriteLine(i);
      }
    }
    public static void Main(string[] args) {
      Int[] ageArr = {
        10,
        20,
        30,
        40
      };
      PrintAgeInfo(ageArr);
    }
  }
}

 

The output will be:

10
20
30
40

 

Anonymous Methods:

An anonymous method is a method that doesn’t have any name.
It is used when the user wants to create an inline method. It uses a delegate keyword to define.

Syntax:

delegate(){
// Code..
};

 

Example:

 

using System;
namespace SampleProgram {
  class MySample {    
    public delegate void GetArea(int x, int y);
    public static void Main(string[] args) {
      GetArea area = delegate(int X, int Y) {
        Console.WriteLine($”Area is: {
          X * Y
        }”);
      }
      area(20, 30);
    }
  }
}

 

The output will be:

Area is:600

 

Related Tutorials