Methods In C#

Methods are very useful in C#. Since any code is to be used at many places, we can define that in a method and can use the code only by calling that method to the different places.
 
Method signature

A method signature contains various things, which are stated below.
  • [Attributes]    (e.g.: [WebMethod])
  • Public void TestMethod(string parameters)
Explanation
 
In the method shown above, Public signature defines the Access modifier.

Void defines the no return type (in case of void, No return type is there and In case of string, its string) of the method.

If the return type is defined as string, then the method must return some string as its output. If we do not want a method to return anything, we can define it as void.

TestMethod

It defines the name of that method.

The string parameters defines the parameter that this method will expect during a call. 
  1. //On the basis of the way we use to call a method from different places, we can define the methods in two types:  
  2. //1. Instance Method: We need to create the object of class inwhich method is defined and then by using that  
  3. // object we can invoke the method.  
  4. //For e.g.  
  1.   class Program  
  2.     {  
  3. public static void Main(string[] args)  
  4.         {  
  5.   InstanceMethodClass  p = new InstanceMethodClass();  
  6.             p.InstanceMethod();  
  7. }  
  8.   
  9. }  
  10.   
  11. public class InstanceMethodClass  
  12.     {  
  13. //This is an Instance method.  
  14. public void InstanceMethod()  
  15.         {  
  16.             Console.WriteLine("This is a test instance method.");  
  17.         }  
  18. }  
 
 
See the image above, if we try to invoke any method by creating an instance of the class then only the instance methods will be visible in the intelligence list.

Static Method

We do not need to create the object of the class in which method is defined. We can invoke the method by using it directly.
  1.    class Program  
  2.     {  
  3.         public static void Main(string[] args)  
  4.         {  
  5.             ClassForStaticMethod.StaticMethod();  
  6.         }  
  7. }  
  8.   
  9.  public class ClassForStaticMethod  
  10.     {  
  11.            public static void StaticMethod()  
  12.         {   
  13.             Console.WriteLine("This is a test static method.");  
  14.         }  
  15.     }  
 
 
As we saw in the screenshot shown above, if we try to invoke any method directly by using the name of the class instead of instance of class, only static methods will get displayed in the intelligence list. 
 
Let's take an example of method with the parameters.
  1.    
  2. class Program  
  3.     {  
  4.         public static void Main(string[] args)  
  5.         {  
  6. InstanceMethodClass i=new InstanceMethodClass();  
  7. i.PrintParameter("Argument passed.");  
  8. }  
  9. }  
  10.  public class InstanceMethodClass{  
  11.   
  12.   public void PrintParameter(string parameter)  
  13.         {  
  14.             Console.WriteLine(parameter);  
  15.         }  
  16. }  
In the example shown above, we need to pass a string argument because "PrintParameter" method expects the string as parameter.
 
Let's take an example of return type method.
  1.    class Program  
  2.     {  
  3.         public static void Main(string[] args)  
  4.         {  
  5. InstanceMethodClass i=InstanceMethodClass();  
  6.  int sum=i.ReturnSum(2,6);  
  7.             Console.WriteLine(sum);  
  8.   
  9. }  
  10. }  
  11.   
  12.  public class InstanceMethodClass  
  13.     {  
  14. public  int ReturnSum(int a,int b)  
  15.         {  
  16.             int sum = a + b;  
  17.             return sum;  
  18.         }  
  19. }  
In the example shown above, we used int as return type instead of void. Thus, the method must return an integer. When we invoke this method, we can get the returned result in an Int variable.
 
In the two examples stated above, it will remain the same for static method but the only difference is the method needs to be defined as static and invoked without using a class instance. 
 
Thank you.