NenTang.vn
.NETLập trình căn bản C#
Tạo phương thức/hàm trong C#
Nhập môn Lập trình C#
Thời lượng: 35 phútĐọc khoảng: 8 phút
Danh mục hình và bảng
Từ viết tắt
Nội dung bài học không khai báo từ viết tắt bằng thẻ abbr.
Nội dung bài học
A method is a group of statements that together perform a task. Every C# program has at least one class with a method named Main.
To use a method, you need to −
Following are the various elements of a method −
When the above code is compiled and executed, it produces the following result −
You can also call public method from other classes by using the instance of the class. For example, the method FindMax belongs to the NumberManipulatorclass, you can call it from another class Test.
When the above code is compiled and executed, it produces the following result −
When the above code is compiled and executed, it produces the following result −
When the above code is compiled and executed, it produces the following result −
It shows that there is no change in the values though they had changed inside the function.
When the above code is compiled and executed, it produces the following result −
It shows that the values have changed inside the swap function and this change reflects in the Main function.
When the above code is compiled and executed, it produces the following result −
The variable supplied for the output parameter need not be assigned a value. Output parameters are particularly useful when you need to return values from a method through the parameters without assigning an initial value to the parameter. Go through the following example, to understand this −
When the above code is compiled and executed, it produces the following result −
- Define the method
- Call the method
Defining Methods in C#
When you define a method, you basically declare the elements of its structure. The syntax for defining a method in C# is as follows −shellscript
- Access Specifier − This determines the visibility of a variable or a method from another class.
- Return type − A method may return a value. The return type is the data type of the value the method returns. If the method is not returning any values, then the return type is void.
- Method name − Method name is a unique identifier and it is case sensitive. It cannot be same as any other identifier declared in the class.
- Parameter list − Enclosed between parentheses, the parameters are used to pass and receive data from a method. The parameter list refers to the type, order, and number of the parameters of a method. Parameters are optional; that is, a method may contain no parameters.
- Method body − This contains the set of instructions needed to complete the required activity.
Example
Following code snippet shows a function FindMax that takes two integer values and returns the larger of the two. It has public access specifier, so it can be accessed from outside the class using an instance of the class.csharp
Calling Methods in C#
You can call a method using the name of the method. The following example illustrates this −csharp
shellscript
csharp
csharp
Recursive Method Call
A method can call itself. This is known as recursion. Following is an example that calculates factorial for a given number using a recursive function −csharp
shellscript
Passing Parameters to a Method
When method with parameters is called, you need to pass the parameters to the method. There are three ways that parameters can be passed to a method −| Sr.No. | Mechanism & Description |
|---|---|
| 1 | Value parametersThis method copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument. |
| 2 | Reference parametersThis method copies the reference to the memory location of an argument into the formal parameter. This means that changes made to the parameter affect the argument. |
| 3 | Output parametersThis method helps in returning more than one value. |
1. Value parameters
This is the default mechanism for passing parameters to a method. In this mechanism, when a method is called, a new storage location is created for each value parameter. The values of the actual parameters are copied into them. Hence, the changes made to the parameter inside the method have no effect on the argument. The following example demonstrates the concept −csharp
shellscript
2. Reference parameters
A reference parameter is a reference to a memory location of a variable. When you pass parameters by reference, unlike value parameters, a new storage location is not created for these parameters. The reference parameters represent the same memory location as the actual parameters that are supplied to the method. You can declare the reference parameters using the ref keyword. The following example demonstrates this −csharp
shellscript
3. Output parameters
A return statement can be used for returning only one value from a function. However, using output parameters, you can return two values from a function. Output parameters are similar to reference parameters, except that they transfer data out of the method rather than into it. The following example illustrates this −csharp
csharp
csharp
csharp

