C# methods
#One method is to organize some related statements together into a block of statements to perform a task. Every C# program has at least one class with a Main method.
To use a method, you need to:
Define the method
Call the method
Defining methods in C
#When you define a method, you are basically declaring the elements of its structure. In C#, the syntax for defining a method is as follows:
<Access Specifier> <Return Type> <Method Name>(Parameter List) { Method Body }
The following are the various elements of the method:
Access Specifier: access modifier, this Determines the visibility of a variable or method to another class.
Return type: Return type, a method can return a value. The return type is the data type of the value returned by the method. If the method returns no value, the return type is void.
Method name: The method name is a unique identifier and is case-sensitive. It cannot be the same as other identifiers declared in the class.
Parameter list: Parameter list, enclosed in parentheses, is used to pass and receive method data. The parameter list refers to the method's parameter types, order, and number. Parameters are optional, that is, a method may contain no parameters.
Method body: The method body contains the set of instructions required to complete the task.
Example
The code snippet below shows a function FindMax that accepts two integer values and returns the larger of the two . It has public access modifier, so it can be accessed from outside the class using an instance of the class.
class NumberManipulator { public int FindMax(int num1, int num2) { /* 局部变量声明 */ int result; if (num1 > num2) result = num1; else result = num2; return result; } ... }
Calling methods in C
#You can call a method using its name. The following example demonstrates this:
using System; namespace CalculatorApplication { class NumberManipulator { public int FindMax(int num1, int num2) { /* 局部变量声明 */ int result; if (num1 > num2) result = num1; else result = num2; return result; } static void Main(string[] args) { /* 局部变量定义 */ int a = 100; int b = 200; int ret; NumberManipulator n = new NumberManipulator(); //调用 FindMax 方法 ret = n.FindMax(a, b); Console.WriteLine("最大值是: {0}", ret ); Console.ReadLine(); } } }
When the above code is compiled and executed, it produces the following results:
最大值是: 200
You can also use an instance of a class from another class Call public methods of other classes. For example, method FindMax belongs to class NumberManipulator and you can call it from another class Test.
using System; namespace CalculatorApplication { class NumberManipulator { public int FindMax(int num1, int num2) { /* 局部变量声明 */ int result; if (num1 > num2) result = num1; else result = num2; return result; } } class Test { static void Main(string[] args) { /* 局部变量定义 */ int a = 100; int b = 200; int ret; NumberManipulator n = new NumberManipulator(); //调用 FindMax 方法 ret = n.FindMax(a, b); Console.WriteLine("最大值是: {0}", ret ); Console.ReadLine(); } } }
When the above code is compiled and executed, it produces the following results:
最大值是: 200
Recursive method call
A method can call itself. This is called recursion. The following example uses a recursive function to calculate the factorial of a number:
using System; namespace CalculatorApplication { class NumberManipulator { public int factorial(int num) { /* 局部变量定义 */ int result; if (num == 1) { return 1; } else { result = factorial(num - 1) * num; return result; } } static void Main(string[] args) { NumberManipulator n = new NumberManipulator(); //调用 factorial 方法 Console.WriteLine("6 的阶乘是: {0}", n.factorial(6)); Console.WriteLine("7 的阶乘是: {0}", n.factorial(7)); Console.WriteLine("8 的阶乘是: {0}", n.factorial(8)); Console.ReadLine(); } } }
When the above code is compiled and executed, it produces the following results:
6 的阶乘是: 720 7 的阶乘是: 5040 8 的阶乘是: 40320
Parameter passing
When calling a method with parameters, you need to pass the parameters to the method. In C#, there are three ways to pass parameters to methods:
Method | Description |
---|---|
Value parameter | This method copies the actual value of the parameter to the function The formal parameters, actual parameters and formal parameters use two different values in memory. In this case, when the value of the formal parameter changes, the value of the actual parameter will not be affected, thus ensuring the security of the actual parameter data. |
Reference parameters | This method copies the reference to the memory location of the parameter to the formal parameter. This means that when the value of the formal parameter changes, the value of the actual parameter also changes. |
Output parameters | This method can return multiple values. |
#Pass parameters by value
This is the default way to pass parameters. In this way, when a method is called, a new storage location is created for each value parameter.
The value of the actual parameter will be copied to the formal parameter. The actual parameter and the formal parameter use two different values in memory. Therefore, when the value of the formal parameter changes, the value of the actual parameter will not be affected, thus ensuring the security of the actual parameter data. The following example demonstrates this concept:
using System; namespace CalculatorApplication { class NumberManipulator { public void swap(int x, int y) { int temp; temp = x; /* 保存 x 的值 */ x = y; /* 把 y 赋值给 x */ y = temp; /* 把 temp 赋值给 y */ } static void Main(string[] args) { NumberManipulator n = new NumberManipulator(); /* 局部变量定义 */ int a = 100; int b = 200; Console.WriteLine("在交换之前,a 的值: {0}", a); Console.WriteLine("在交换之前,b 的值: {0}", b); /* 调用函数来交换值 */ n.swap(a, b); Console.WriteLine("在交换之后,a 的值: {0}", a); Console.WriteLine("在交换之后,b 的值: {0}", b); Console.ReadLine(); } } }
When the above code is compiled and executed, it produces the following results:
在交换之前,a 的值:100 在交换之前,b 的值:200 在交换之后,a 的值:100 在交换之后,b 的值:200
The results show that even if the value is changed within the function, the value Nothing has changed.
Passing parameters by reference
A reference parameter is a reference to the memory location of a variable. When parameters are passed by reference, unlike value parameters, it does not create a new storage location for those parameters. Reference parameters represent the same memory location as the actual parameters supplied to the method.
In C#, use the ref keyword to declare reference parameters. The following example demonstrates this:
using System; namespace CalculatorApplication { class NumberManipulator { public void swap(ref int x, ref int y) { int temp; temp = x; /* 保存 x 的值 */ x = y; /* 把 y 赋值给 x */ y = temp; /* 把 temp 赋值给 y */ } static void Main(string[] args) { NumberManipulator n = new NumberManipulator(); /* 局部变量定义 */ int a = 100; int b = 200; Console.WriteLine("在交换之前,a 的值: {0}", a); Console.WriteLine("在交换之前,b 的值: {0}", b); /* 调用函数来交换值 */ n.swap(ref a, ref b); Console.WriteLine("在交换之后,a 的值: {0}", a); Console.WriteLine("在交换之后,b 的值: {0}", b); Console.ReadLine(); } } }
When the above code is compiled and executed, it produces the following results:
在交换之前,a 的值:100 在交换之前,b 的值:200 在交换之后,a 的值:200 在交换之后,b 的值:100
The results show that, swap Inside the function The value has changed, and this change can be reflected in the Main function.
Pass parameters by output
The return statement can be used to return only a value from a function. However, you can use output parameters to return two values from the function. Output parameters assign the data output by the method to themselves, and are otherwise similar to reference parameters.
The following example demonstrates this:
using System; namespace CalculatorApplication { class NumberManipulator { public void getValue(out int x ) { int temp = 5; x = temp; } static void Main(string[] args) { NumberManipulator n = new NumberManipulator(); /* 局部变量定义 */ int a = 100; Console.WriteLine("在方法调用之前,a 的值: {0}", a); /* 调用函数来获取值 */ n.getValue(out a); Console.WriteLine("在方法调用之后,a 的值: {0}", a); Console.ReadLine(); } } }
When the above code is compiled and executed, it produces the following results:
在方法调用之前,a 的值: 100 在方法调用之后,a 的值: 5
Variables provided to the output parameters No assignment is required. Output parameters are particularly useful when you need to return a value from a method that does not specify an initial value for the parameter. Please look at the following example to understand this:
using System; namespace CalculatorApplication { class NumberManipulator { public void getValues(out int x, out int y ) { Console.WriteLine("请输入第一个值: "); x = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("请输入第二个值: "); y = Convert.ToInt32(Console.ReadLine()); } static void Main(string[] args) { NumberManipulator n = new NumberManipulator(); /* 局部变量定义 */ int a , b; /* 调用函数来获取值 */ n.getValues(out a, out b); Console.WriteLine("在方法调用之后,a 的值: {0}", a); Console.WriteLine("在方法调用之后,b 的值: {0}", b); Console.ReadLine(); } } }
When the above code is compiled and executed, it produces the following results (depending on user input):
请输入第一个值: 7 请输入第二个值: 8 在方法调用之后,a 的值: 7 在方法调用之后,b 的值: 8