C # methods
One way is to put together some related statements that are used to execute a block of statements for a task. Each C # program has at least one class with the Main method.
To use a method, you need to:
Defining methods
Calling methods
Defining Methods in C #
When a method is defined, it is fundamentally the element that declares its structure. In C #, the syntax for defining a method is as follows:
<access specifier> <return type> <method name> (Parameter List) { Method Body}
Here are the various elements of the method:
Access specifier: A modifier that determines the visibility of a variable or method for another class.
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 does not return any values, the return type is void.
Method Name: is a unique identifier and is case-sensitive. It cannot be the same as the other identifiers declared in the class.
Parameter list: parameter lists, enclosed in parentheses, that are data that is used to pass and receive methods. The parameter list refers to the parameter type, order, and quantity of the method. Parameters are optional, that is, a method may not contain parameters.
Method body: The body of methods that contains the set of instructions required to complete the task.
Instance
The following code snippet shows a function, Findmax, that accepts two integer values and returns a larger value in two. It has the 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) {/ * local variable declaration */ int result; if (Num1 > num2) result = NUM1; else result = num2; return result; } ...}
Calling Methods in C #
You can invoke a method by using the method name. The following example demonstrates this:
Using System;namespace calculatorapplication{ class Numbermanipulator {public int findmax (int num1, int NUM2) {/ * local variable declaration */ int result; if (Num1 > num2) result = NUM1; else result = num2; return result; } static void Main (string[] args) {/ * local variable definition */ int a = +; int b = n; int ret; Numbermanipulator n = new Numbermanipulator (); Call the Findmax method ret = N.findmax (A, b); Console.WriteLine ("Maximum value is: {0}", ret); Console.ReadLine ();}}}
When the above code is compiled and executed, it produces the following results:
Maximum value is: 200
You can also use an instance of a class to invoke the public method of another class from another class. For example, the method Findmax belongs to the Numbermanipulator class, and you can call it from another class Test.
Using System;namespace calculatorapplication{ class Numbermanipulator {public int findmax (int num1, int NUM2) {/ * local variable declaration */ int result; if (Num1 > num2) result = NUM1; else result = num2; return result; } } Class Test { static void Main (string[] args) {/ * local variable definition */ int a = +; int b = n; int ret; Numbermanipulator n = new Numbermanipulator (); Call the Findmax method ret = N.findmax (A, b); Console.WriteLine ("Maximum value is: {0}", ret); Console.ReadLine ();}}}
When the above code is compiled and executed, it produces the following results:
Maximum value is: 200
Recursive method invocation
A method can invoke 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) {/ * local variable definition */ int result; if (num = = 1) { return 1; } else { result = factorial (num-1) * NUM; return result; } } static void Main (string[] args) { numbermanipulator n = new Numbermanipulator (); Call the Factorial method Console.WriteLine ("6 factorial is: {0}", n.factorial (6)); Console.WriteLine (The factorial of "7 is: {0}", n.factorial (7)); Console.WriteLine (The factorial of "8 is: {0}", N.factorial (8)); Console.ReadLine ();}}}
When the above code is compiled and executed, it produces the following results:
The factorial of 6 is: the factorial of 7207 is: the factorial of 50408 is: 40320
Parameter passing
When calling a method with parameters, you need to pass parameters to the method. In C #, there are three ways to pass parameters to a method:
Way
Describe
The value parameter copies the actual value of the parameter to the formal parameter of the function, and the arguments and parameters use two different memory values. In this case, when the value of the parameter changes, the value of the argument is not affected, thus guaranteeing the security of the argument data.
Reference parameter This way copies the reference of the parameter's memory location to the formal parameter. This means that when the value of the parameter changes, the value of the argument is also changed.
Output parameters This way you can return multiple values.
Passing 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 argument is copied to the parameter, and the arguments and parameters use two different values in memory. Therefore, when the value of a parameter changes, the value of the argument is not affected, thus guaranteeing 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; /* Save the value of x */ x = y; /* Assign y to x */ y = temp,/* Assign temp to Y * /} static void Main (string[] args) { Numbermanipulator n = new Numbermanipulator (); /* local variable definition */ int a = +; int b = n; Console.WriteLine ("The value of a before swapping: {0}", a); Console.WriteLine ("Before swapping, value of B: {0}", b); /* Call function to Exchange values * /N.swap (A, b); Console.WriteLine ("After Exchange, the value of a: {0}", a); Console.WriteLine ("After Exchange, value of B: {0}", b); Console.ReadLine ();}}}
When the above code is compiled and executed, it produces the following results:
Before swapping, the value of a: 100 before the exchange, the value of B: 200 after the exchange, the value of a: 100 after the exchange, the value of B: 200
The result shows that even if the value is changed within the function, the value does not change any more.
Passing parameters by reference
A reference parameter is a reference to the memory location of the variable. When passing parameters by reference, unlike the value parameter, it does not create a new storage location for these parameters. The reference parameter represents the same memory location as the actual parameter supplied to the method.
In C #, you declare reference parameters by using the REF keyword. The following example demonstrates this:
Using System;namespace calculatorapplication{ class Numbermanipulator {public void swap (ref int x, ref int Y) { int temp; temp = x; /* Save the value of x */ x = y; /* Assign y to x */ y = temp,/* Assign temp to Y * /} static void Main (string[] args) { Numbermanipulator n = new Numbermanipulator (); /* local variable definition */ int a = +; int b = n; Console.WriteLine ("The value of a before swapping: {0}", a); Console.WriteLine ("Before swapping, value of B: {0}", b); /* Call function to Exchange values * /N.swap (ref a, ref B); Console.WriteLine ("After Exchange, the value of a: {0}", a); Console.WriteLine ("After Exchange, value of B: {0}", b); Console.ReadLine ();}}}
When the above code is compiled and executed, it produces the following results:
Before swapping, the value of a: 100 before the exchange, the value of B: 200 after the exchange, the value of a: 200 after the exchange, the value of B: 100
The result shows that the value in the SWAP function has changed, and the change can be reflected in the Main function.
Pass parameters by output
The return statement can be used to return only one value from a function. However, you can use an output parameter to return two values from a function. The output parameter assigns the data of the method output to itself, and other aspects are similar to the reference parameter.
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 (); /* local variable definition */ int a = +; Console.WriteLine ("The value of a before the method call: {0}", a); /* Call the function to get the value * /N.getvalue (out a); Console.WriteLine ("After the method call, the value of a: {0}", a); Console.ReadLine ();}}}
When the above code is compiled and executed, it produces the following results:
Before the method call, the value of a is: 100 after the method call, the value of a: 5
The variable supplied to the output parameter does not need to be assigned a value. Output parameters are particularly useful when you need to return a value from a method that does not specify an initial value for a parameter. Take a 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 ("Please enter first value:"); x = Convert.ToInt32 (Console.ReadLine ()); Console.WriteLine ("Please enter a second value:"); y = Convert.ToInt32 (Console.ReadLine ()); } static void Main (string[] args) { numbermanipulator n = new Numbermanipulator (); /* local variable definition */ int A, b; /* Call the function to get the value */n.getvalues (out a, out B); Console.WriteLine ("After the method call, the value of a: {0}", a); Console.WriteLine ("After a method call, the value of B: {0}", b); Console.ReadLine ();}}}
When the above code is compiled and executed, it produces the following results (depending on user input):
Please enter the first value: 7 Enter the second value: 8 after the method call, the value of a: 7 after the method call, the value of B: 8
The above is the "C # Tutorial" C # method content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!