In this tutorial, we will learn about the C# method with the help of examples.
A C# method is a collection of statements that are grouped together to perform an operation. When you call the Console.WriteLine() method, for example, the system actually executes several statements in order to display a message on the console.
Now you will learn how to create your own methods with or without return values, invoke a method with or without parameters, and apply method abstraction in the program design.
Creating Method
Considering the following example to explain the syntax of a method −
Syntax
1 2 3 4 5 | public static int methodName(int a, int b) { // body } |
Here,
- public static − modifier
- int − return type
- methodName − name of the method
- a, b − formal parameters
- int a, int b − list of parameters
Method definition consists of a method header and a method body. The same is shown in the following syntax −
Syntax
1 2 3 4 5 | modifier returnType nameOfMethod (Parameter List) { // method body } |
The syntax shown above includes −
- modifier − It defines the access type of the method and it is optional to use.
- returnType − Method may return a value.
- nameOfMethod − This is the method name. The method signature consists of the method name and the parameter list.
- Parameter List − The list of parameters, it is the type, order, and number of parameters of a method. These are optional, method may contain zero parameters.
- method body − The method body defines what the method does with the statements.
Example
Here is the source code of the above defined method called min(). This method takes two parameters num1 and num2 and returns the maximum between the two −
1 2 3 4 5 6 7 8 9 10 11 12 | public static int minFunction(int n1, int n2) { int min; if (n1 > n2) min = n2; else min = n1; return min; } |
For using a method, it should be called. There are two ways in which a method is called i.e., method returns a value or returning nothing (no return value).
The process of method calling is simple. When a program invokes a method, the program control gets transferred to the called method. This called method then returns control to the caller in two conditions, when −
- the return statement is executed.
- it reaches the method ending closing brace.
The methods returning void is considered as call to a statement. Lets consider an example −
1 2 3 | Console.WriteLine("This is csharp-console-examples.com"); |
The method returning value can be understood by the following example −
1 2 3 | int result = sum(6, 9); |
Following is the example to demonstrate how to define a method and how to call it −
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | internal class Program { public static int minFunction(int n1, int n2) { int min; if (n1 > n2) min = n2; else min = n1; return min; } static void Main(string[] args) { int a = 4; int b = 7; int c = minFunction(a, b); Console.WriteLine("Minimum Value : "+c); Console.ReadKey(); } } |
Example
The following program shows an example of passing parameter by value. The values of the arguments remains the same even after the method invocation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | internal class Program { public static void swapFunction(int a, int b) { Console.WriteLine("Before swapping(Inside), a = " + a + " b = " + b); // Swap n1 with n2 int c = a; a = b; b = c; Console.WriteLine("After swapping(Inside), a = " + a + " b = " + b); } static void Main(string[] args) { int a = 30; int b = 45; Console.WriteLine("Before swapping, a = " + a + " and b = " + b); // Invoke the swap method swapFunction(a, b); Console.WriteLine("\n**Now, Before and After swapping values will be same here**:"); Console.WriteLine("After swapping, a = " + a + " and b is " + b); Console.ReadKey(); } } |
Output:
Method Overloading
When a class has two or more methods by the same name but different parameters, it is known as method overloading. It is different from overriding. In overriding, a method has the same method name, type, number of parameters, etc.
Let’s consider the example discussed earlier for finding minimum numbers of integer type. If, let’s say we want to find the minimum number of double type. Then the concept of overloading will be introduced to create two or more methods with the same name but different parameters.
The following example explains the same −
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | internal class Program { public static int minFunction(int n1, int n2) { int min; if (n1 > n2) min = n2; else min = n1; return min; } // for double public static double minFunction(double n1, double n2) { double min; if (n1 > n2) min = n2; else min = n1; return min; } static void Main(string[] args) { int a = 19; int b = 8; double c = 3.2; double d = 7.4; int result1 = minFunction(a, b); // same function name with different parameters double result2 = minFunction(c, d); Console.WriteLine("Minimum Value = " + result1); Console.WriteLine("Minimum Value = " + result2); Console.ReadKey(); } } |
This will produce the following result −
Output
Overloading methods makes program readable. Here, two methods are given by the same name but with different parameters. The minimum number from integer and double types is the result.