Learn All About Methods In C#

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,
  1. Parameter-less method
  2. Parameterized method
  3. Parameter and return method
  4. Call by value method
  5. Reference method
  6. Out parameter method
  7. Method overriding
  8. Method overloading
  9. Method hiding
  10. Abstract method
  11. Virtual method
  12. 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.
  1. using System;  
  2.    
  3. namespace MethodExamples  
  4. {  
  5.     class ParameterLessMethod  
  6.     {  
  7.         protected void Message()  
  8.         {  
  9.             Console.WriteLine("Hello World!");  
  10.         }  
  11.    
  12.         static void Main()  
  13.         {  
  14.             var p = new ParameterLessMethod();  
  15.             p.Message();  
  16.             Console.ReadLine();  
  17.         }  
  18.     }  
  19. }  

What is parameterized method? 

 
A method which contains at least one or more parameters is called parameterized method.
  1. using System;  
  2.    
  3. namespace MethodExamples  
  4. {  
  5.     class ParameterMethod  
  6.     {  
  7.         public void PrintMessage(string message)  
  8.         {  
  9.             Console.WriteLine("Hello " + message);  
  10.         }  
  11.    
  12.         static void Main()  
  13.         {  
  14.             var p = new ParameterMethod();  
  15.             p.PrintMessage("Farhan Ahmed");  
  16.             Console.ReadLine();  
  17.         }  
  18.     }  
  19. }  

What is parameter and return method? 

 
A method that does not return any value specifies the void type as a return type.
  1. using System;  
  2.    
  3. namespace MethodExamples  
  4. {  
  5.     class ParameterAndReturnMethod  
  6.     {  
  7.         protected int MethodValue(int value)  
  8.         {  
  9.             Console.WriteLine("Entered value is:"+value);  
  10.             return value;  
  11.         }  
  12.    
  13.         static void Main()  
  14.         {  
  15.             var p = new ParameterAndReturnMethod();  
  16.             p.MethodValue(15);  
  17.             Console.ReadLine();  
  18.         }  
  19.     }  
  20. }  

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.
  1. using System;  
  2.   
  3. namespace MethodExamples  
  4. {  
  5.     class CallByValueMethod  
  6.     {  
  7.         public void ShowValue(int value)  
  8.         {  
  9.             value *= value;  
  10.             Console.WriteLine("Value of method:" + value);               
  11.         }  
  12.   
  13.         static void Main(string[] args)  
  14.         {  
  15.             int value = 20;  
  16.             var p = new CallByValueMethod();  
  17.             Console.WriteLine("Value before method called:"+value);  
  18.             p.ShowValue(value);  
  19.             Console.WriteLine("Value after method called:" + value);  
  20.             Console.ReadLine();  
  21.         }  
  22.     }  
  23. }  

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.
  1. using System;  
  2.    
  3. namespace MethodExamples  
  4. {  
  5.     class CallByReferenceMethod  
  6.     {  
  7.         protected void PrintNumber(ref int number)  
  8.         {  
  9.             number *= number;  
  10.             Console.WriteLine("Value of reference method: " + number);  
  11.         }  
  12.    
  13.         static void Main()  
  14.         {  
  15.             int number = 100;  
  16.             var p = new CallByReferenceMethod();  
  17.             Console.WriteLine("Value before reference method called: " + number);  
  18.             p.PrintNumber(ref number);  
  19.             Console.WriteLine("Reflected value afrter method called: " + number);  
  20.             Console.ReadLine();  
  21.         }  
  22.     }  
  23. }  

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.
  1. using System;  
  2.   
  3. namespace MethodExamples  
  4. {  
  5.     class OutParameterMethod  
  6.     {  
  7.         protected void PrintNumber(out int number)  
  8.         {  
  9.             int squre=0;  
  10.             number = squre;  
  11.             number *= number;  
  12.             Console.WriteLine("Please enter number");  
  13.             squre = Convert.ToInt32(Console.ReadLine());  
  14.             Console.WriteLine("Value when method called out parameter: " + number);  
  15.         }  
  16.         static void Main()  
  17.         {  
  18.             int number = 50;  
  19.             var p = new OutParameterMethod();  
  20.             Console.WriteLine("Value before out parameter method called: " + number);  
  21.             p.PrintNumber(out number);  
  22.             Console.WriteLine("Value after out parameter method called: " + number);  
  23.             Console.ReadLine();  
  24.         }  
  25.     }  
  26. }  

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.
  1. using System;  
  2.    
  3. namespace MethodExamples  
  4. {  
  5.     class ExtentionMethod  
  6.     {  
  7.         static void Main()  
  8.         {  
  9.             int number;  
  10.    
  11.             Console.WriteLine("Enter numer");  
  12.             number = Convert.ToInt32(Console.ReadLine());  
  13.             bool result = number.IsEven();  
  14.             Console.WriteLine(result);  
  15.             Console.ReadLine();  
  16.         }  
  17.     }  
  18.    
  19.     public static class Helper  
  20.     {  
  21.         public static bool IsEven(this int number)  
  22.         {  
  23.             return (number%2==0);  
  24.         }  
  25.     }  
  26. }  

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.
  1. using System;  
  2.    
  3. namespace MethodExamples  
  4. {  
  5.     class AnonymousMethod  
  6.     {  
  7.         public delegate void Print(int value);  
  8.    
  9.         static void Main()  
  10.         {  
  11.             Print print = delegate (int val)   
  12.             {  
  13.                 Console.WriteLine("Inside Anonymous method. Value: {0}", val);  
  14.             };  
  15.             print(100);  
  16.    
  17.             Console.ReadLine();  
  18.         }  
  19.     }  
  20. }  

What is method overloading?

 
A method with the same name but the different signature is called method overloading.
  1. using System;  
  2.    
  3. namespace MethodExamples  
  4. {  
  5.     class MethodOverloading  
  6.     {  
  7.         static void Main()  
  8.         {  
  9.             var demo = new Demo();  
  10.             int firstTotal= demo.Add(10, 20);  
  11.             int secondTotal = demo.Add(10, 20, 30);  
  12.             double thirdTotal= demo.Add(10.5, 20.5);  
  13.    
  14.             Console.WriteLine("Total:{0}",firstTotal);  
  15.             Console.WriteLine("Second Total:{0}",secondTotal);  
  16.             Console.WriteLine("Third Total:{0}", thirdTotal);  
  17.    
  18.             Console.ReadLine();  
  19.         }  
  20.     }  
  21.    
  22.     public class Demo  
  23.     {  
  24.         public int Add(int valueOne, int valueTwo)  
  25.         {  
  26.             return valueOne + valueTwo;  
  27.         }  
  28.    
  29.         public int Add(int valueOne, int valueTwo, int valueThree)  
  30.         {  
  31.             return valueOne + valueTwo + valueThree;  
  32.         }  
  33.    
  34.         public double Add(double valueOne, double valueTwo)  
  35.         {  
  36.             return valueOne + valueTwo;  
  37.         }  
  38.     }  
  39. }  

What are the different ways a method can be overloaded?

 
Methods can be overloaded using different data types for a parameter, different order of parameters, and the different number of parameters.
 

What is method overriding?

 
A method with the same name and same parameters is called method overriding.
  1. using System;  
  2.    
  3. namespace MethodExamples  
  4. {  
  5.     class MethodOverriding  
  6.     {  
  7.         static void Main()  
  8.         {  
  9.             var employee = new Employee();  
  10.             employee.PrintFullName();  
  11.             Console.WriteLine("------------");  
  12.             var contactEmployee = new ContactEmployee();  
  13.             contactEmployee.PrintFullName();  
  14.             Console.ReadLine();  
  15.         }  
  16.     }  
  17.    
  18.     public class Employee  
  19.     {  
  20.         public string firstName { get; set;}  
  21.         public string lastName { get; set; }  
  22.          
  23.         public virtual void PrintFullName()  
  24.         {  
  25.             Console.WriteLine("Please enter first name:");  
  26.             firstName = Convert.ToString(Console.ReadLine());  
  27.             Console.WriteLine("Please enter last name:");  
  28.             lastName = Convert.ToString(Console.ReadLine());  
  29.    
  30.             Console.WriteLine("FullName:"+ firstName +" "+ lastName);  
  31.         }  
  32.     }  
  33.    
  34.     public class ContactEmployee:Employee  
  35.     {  
  36.         public override void PrintFullName()  
  37.         {  
  38.             base.PrintFullName();  
  39.         }  
  40.     }  
  41. }  

What is method hiding?

 
A method with the same name and same parameters which hides the method of the base class by using a new keyword is called method hiding.
  1. using System;  
  2.    
  3. namespace MethodExamples  
  4. {  
  5.     class MethodHiding  
  6.     {  
  7.         static void Main()  
  8.         {  
  9.             var customer = new Customer();  
  10.             customer.FullName();  
  11.             Console.WriteLine("----------");  
  12.             var premiumCustomer = new PremiumCustomer();  
  13.             premiumCustomer.FullName();  
  14.    
  15.             Console.ReadLine();  
  16.    
  17.         }  
  18.     }  
  19.    
  20.     public class Customer  
  21.     {  
  22.         public string firstName { get; set; }  
  23.         public string lastName { get; set; }  
  24.    
  25.         public void FullName()  
  26.         {  
  27.             Console.WriteLine("Enter first name");  
  28.             firstName = Convert.ToString(Console.ReadLine());  
  29.             Console.WriteLine("Enter last name");  
  30.             lastName = Convert.ToString(Console.ReadLine());  
  31.    
  32.             Console.WriteLine("Full Name:" + firstName + " " + lastName);  
  33.         }  
  34.     }  
  35.    
  36.     public class PremiumCustomer:Customer  
  37.     {  
  38.         public string middleName { get; set; }  
  39.    
  40.         public new void FullName()  
  41.         {  
  42.             Console.WriteLine("Enter first name");  
  43.             firstName = Convert.ToString(Console.ReadLine());  
  44.             Console.WriteLine("Enter middle name");  
  45.             middleName = Convert.ToString(Console.ReadLine());  
  46.             Console.WriteLine("Enter last name");  
  47.             lastName = Convert.ToString(Console.ReadLine());  
  48.    
  49.             Console.WriteLine("Full Name:" + firstName + " " +middleName +" " + lastName);  
  50.         }  
  51.     }  
  52. }  

What is abstract method?

 
A method with abstract modifier indicates that the method is an abstract method. Abstract methods have no implementation, so the method definition is followed by a semicolon instead of a normal method block. Derived classes of the abstract class must implement all abstract methods.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.    
  7. namespace MethodExamples  
  8. {  
  9.     public abstract class AbstractMethod  
  10.     {  
  11.         public abstract void MyAbstractMethod();  
  12.     }  
  13. }  
Features of abstract method
  1. An abstract method is implicitly a virtual method.
  2. Abstract method declarations are only permitted in abstract classes.
  3. An abstract method declaration provides no actual implementation, there is no method body; the method declaration simply ends with a semicolon and there are no curly braces ({ }) following the signature.
  4. The implementation is provided by a method override, which is a member of a non-abstract class.
  5. It is an error to use the static or virtual modifiers in an abstract method declaration.

What is a virtual method?

 
The virtual keyword is used to modify a method, property, indexer, or event declaration and allow for it to be overridden in a derived class.
 
A Virtual method must always have a default implementation. However, it can be overridden in the derived class, though not mandatory. It can be overridden using the override keyword.
  1. using System;  
  2.    
  3. namespace MethodExamples  
  4. {  
  5.     public class VirtualMethod  
  6.     {  
  7.         public virtual string Message(string msg)  
  8.         {  
  9.             return "Hello" + msg;  
  10.         }  
  11.     }  
  12.    
  13.     public class myClass : VirtualMethod  
  14.     {  
  15.         public override string Message(string msg)  
  16.         {  
  17.             return base.Message(msg);  
  18.         }  
  19.     }  
  20.    
  21.     public class program  
  22.     {  
  23.         static void Main()  
  24.         {  
  25.             var p = new VirtualMethod();  
  26.             string name= p.Message("Farhan Ahmed");  
  27.    
  28.             var p1 = new myClass();  
  29.             string name1= p1.Message("Rahul Sharma");  
  30.    
  31.             Console.WriteLine(name);  
  32.             Console.WriteLine(name1);  
  33.    
  34.             Console.ReadLine();  
  35.         }  
  36.     }  
  37.    
  38. }  

What is the difference between the virtual method and abstract method?

 
Virtual Method
 
The virtual keyword is used to modify a method, property, indexer, or event declaration and allow for it to be overridden in a derived class. A Virtual method must always have a default implementation. However, it can be overridden in the derived class, though not mandatory. It can be overridden using the override keyword.
 
Abstract Method
 
An Abstract method does not have an implementation. It resides inside the abstract class. It is mandatory that the derived class implements the abstract method. An override keyword is not necessary here though it can be used.
 

What is a static method?

 
The static keyword is used to create a static method. The static method is the method that can be called without creating an object of the class. It is referenced by the class name itself or reference to the object of that class.
  1. using System;  
  2.    
  3. namespace MethodExamples  
  4. {  
  5.     static class StaticMethod  
  6.     {  
  7.         public static void PrintValue(int value)  
  8.         {  
  9.             Console.WriteLine("The Value is: " + value);  
  10.         }  
  11.     }  
  12.    
  13.     public class StaticExample  
  14.     {  
  15.         static void Main()  
  16.         {  
  17.             StaticMethod.PrintValue(100);  
  18.             Console.ReadLine();  
  19.         }  
  20.     }  
  21. }  


Recommended Free Ebook
Similar Articles