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.
- class Program
- {
- public static void Main(string[] args)
- {
- InstanceMethodClass p = new InstanceMethodClass();
- p.InstanceMethod();
- }
-
- }
-
- public class InstanceMethodClass
- {
-
- public void InstanceMethod()
- {
- Console.WriteLine("This is a test instance method.");
- }
- }
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.
- class Program
- {
- public static void Main(string[] args)
- {
- ClassForStaticMethod.StaticMethod();
- }
- }
-
- public class ClassForStaticMethod
- {
- public static void StaticMethod()
- {
- Console.WriteLine("This is a test static method.");
- }
- }
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.
-
- class Program
- {
- public static void Main(string[] args)
- {
- InstanceMethodClass i=new InstanceMethodClass();
- i.PrintParameter("Argument passed.");
- }
- }
- public class InstanceMethodClass{
-
- public void PrintParameter(string parameter)
- {
- Console.WriteLine(parameter);
- }
- }
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.
- class Program
- {
- public static void Main(string[] args)
- {
- InstanceMethodClass i=InstanceMethodClass();
- int sum=i.ReturnSum(2,6);
- Console.WriteLine(sum);
-
- }
- }
-
- public class InstanceMethodClass
- {
- public int ReturnSum(int a,int b)
- {
- int sum = a + b;
- return sum;
- }
- }
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.