A method refers to a block of code (or a group of statements) that performs a specific task. It takes some inputs, does some computation based on that input and returns the output. They are also known as functions.
The use of function in a program helps in many ways such as reuse of code, Optimization of Code. The function is declared within the curly braces {}.
Every C# program has at least one function which is always executed and that is the main
function from where the execution of the program begins.
In order to use a method in a program, you need to:
- define a method
- call a method
Defining Methods in C#
Defining a method means to write its structure with all of its components. Generally, the function is defined in following way in C#:
1 2 3 4 | <Access Specifier> <Return Type> <Method Name>(Parameter List) { //Method Body } |
Followings are the list of component present in a method.
- Access Specifier: It defines its visibility to the other class, which means it specifies the variables and methods accessibility in the program.
- Return type: A function may return a value unless it is a void function, which does not return any type of value. return_type is a data type of the function. It could return arithmetic values (int, float, etc.), pointers, structures, etc.
- Function name: Function name refers to identifiers through which function can be identified uniquely. It is a function’s name that is stated to function by following some naming rules.
- Parameter list: Parameter is also referred to as Argument. We can pass the values through the parameter when the function is invoked. The parameter list refers to the type, order, and number of the parameters of a function
- Function Body: The function body is enclosed within the curly braces’{}’. It defines the task of a method within the curly braces.
Remember that you can also leave the parameter list empty if you are not passing any value to the function.
Calling Methods in C#
To call a method means to execute the method and to do that we need to write a function name by passing the values as parameters (if any) within the two parentheses () and semicolon.
Following is the syntax t call a function.
1 | function_name( parameter_list ); |
Example: C# example of method
let us see an example in C# where we create an addition function and pass the value to add and return the result to the main function. We will see the use of defining and calling a method in C#.
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 | using System; namespace MethodExam { class Program { //defining a method public int addFunc(int x, int y) { return x+y; } //main Function static void Main(string[] args) { int result; // Creating Object Program program = new Program(); // Calling Function result = program.addFunc(5, 10); Console.WriteLine("The result of addition: {0}", result); } } } |
1 2 3 | //Output: The result of addition: 15 |
Example:
Let us see the use of Access Specifier in the C# program. The program below will have two classes and the method of one class can be accessed by another class by using the instance of the class. We need to use the public Access Specifier.
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 | using System; namespace MethodExample { //class1 class Class1 { // User defined function without return type public int largestFunc(int num1, int num2) { if (num1 > num2) return num1; else return num2; } } //class2 class Class2 { static void Main(string[] args) { int largest; // Creating Object Class1 class1 = new Class1(); // Calling Function largest = class1.largestFunc(100, 80); Console.WriteLine("The largest of two number is: {0}", largest); } } } |
Output:
1 | The largest of two number is: 100 |
How to pass Parameter in Methods?
We need to pass the values to a method in order to calculate just like an example shown above, we can do that in three different ways in C#.
- call by value
- call by reference
- out parameter
We will got through each of them in next tutorial.