What is the method?
The method is a block of code that contains a series of the statement.
Different types of methods in C# programming,
- Parameter-less method
- Parameterized method
- Parameter and return method
- Call by value method
- Reference method
- Out parameter method
- Method overriding
- Method overloading
- Method hiding
- Abstract method
- Virtual method
- Static method
What is a parameter-less method?
A method which does not have any parameter is called the parameter-less method. It is a block of code with the statement.
- using System;
- namespace MethodExamples
- {
- class ParameterLessMethod
- {
- protected void Message()
- {
- Console.WriteLine("Hello World!");
- }
- static void Main()
- {
- var p = new ParameterLessMethod();
- p.Message();
- Console.ReadLine();
- }
- }
- }
What is parameterized method?
A method which contains at least one or more parameters is called parameterized method.
- using System;
- namespace MethodExamples
- {
- class ParameterMethod
- {
- public void PrintMessage(string message)
- {
- Console.WriteLine("Hello " + message);
- }
- static void Main()
- {
- var p = new ParameterMethod();
- p.PrintMessage("Farhan Ahmed");
- Console.ReadLine();
- }
- }
- }
What is parameter and return method?
A method that does not return any value specifies the void type as a return type.
- using System;
- namespace MethodExamples
- {
- class ParameterAndReturnMethod
- {
- protected int MethodValue(int value)
- {
- Console.WriteLine("Entered value is:"+value);
- return value;
- }
- static void Main()
- {
- var p = new ParameterAndReturnMethod();
- p.MethodValue(15);
- Console.ReadLine();
- }
- }
- }
What is call by value method?
The value type parameters pass a copy of the original value to the method rather than reference. It does not change or modify the original value. A change made in passed value does not alter the actual value. Here is the example of a call by value method.
- using System;
- namespace MethodExamples
- {
- class CallByValueMethod
- {
- public void ShowValue(int value)
- {
- value *= value;
- Console.WriteLine("Value of method:" + value);
- }
- static void Main(string[] args)
- {
- int value = 20;
- var p = new CallByValueMethod();
- Console.WriteLine("Value before method called:"+value);
- p.ShowValue(value);
- Console.WriteLine("Value after method called:" + value);
- Console.ReadLine();
- }
- }
- }
What is ref or reference method?
The ref keyword is used to pass and return the reference value. Any value change that is passed as a reference also changes and reflects it.
- using System;
- namespace MethodExamples
- {
- class CallByReferenceMethod
- {
- protected void PrintNumber(ref int number)
- {
- number *= number;
- Console.WriteLine("Value of reference method: " + number);
- }
- static void Main()
- {
- int number = 100;
- var p = new CallByReferenceMethod();
- Console.WriteLine("Value before reference method called: " + number);
- p.PrintNumber(ref number);
- Console.WriteLine("Reflected value afrter method called: " + number);
- Console.ReadLine();
- }
- }
- }
What is out or out parameter method?
The out keyword is used to pass the argument as out parameter. It is the same as a reference type, except that it does not require initialization before it passes to the method. It is useful when we need to return multiple values in a method.
- using System;
- namespace MethodExamples
- {
- class OutParameterMethod
- {
- protected void PrintNumber(out int number)
- {
- int squre=0;
- number = squre;
- number *= number;
- Console.WriteLine("Please enter number");
- squre = Convert.ToInt32(Console.ReadLine());
- Console.WriteLine("Value when method called out parameter: " + number);
- }
- static void Main()
- {
- int number = 50;
- var p = new OutParameterMethod();
- Console.WriteLine("Value before out parameter method called: " + number);
- p.PrintNumber(out number);
- Console.WriteLine("Value after out parameter method called: " + number);
- Console.ReadLine();
- }
- }
- }
What is extension method?
An extension method enables us to add methods to existing types without creating a new derived type, recompiling, or modifying the original types.
- using System;
- namespace MethodExamples
- {
- class ExtentionMethod
- {
- static void Main()
- {
- int number;
- Console.WriteLine("Enter numer");
- number = Convert.ToInt32(Console.ReadLine());
- bool result = number.IsEven();
- Console.WriteLine(result);
- Console.ReadLine();
- }
- }
- public static class Helper
- {
- public static bool IsEven(this int number)
- {
- return (number%2==0);
- }
- }
- }
What is anonymous method?
An anonymous method is an inline unnamed method in the code. It is created using the delegate keyword and doesn't require the name and return type.
- using System;
- namespace MethodExamples
- {
- class AnonymousMethod
- {
- public delegate void Print(int value);
- static void Main()
- {
- Print print = delegate (int val)
- {
- Console.WriteLine("Inside Anonymous method. Value: {0}", val);
- };
- print(100);
- Console.ReadLine();
- }
- }
- }

Guest UserPosted Jun 28, 2020, 12:11 AM
Thanks for sharing such an important article. Appreciate the efforts.
Eslam MahfouzPosted Aug 18, 2019, 3:16 AM
Great article
Amit MohantyPosted Aug 17, 2019, 11:03 PM
Nice article
Jin NecesarioPosted Aug 17, 2019, 7:11 PM
Great article. A good summary for different types of methods.
Rajanikant HawaldarPosted Aug 16, 2019, 12:41 PM
nice article