1, in the main () function, we call the test () function, we call the main () function called the caller,
The tube test () function is called the callee.
If the callee wants to get the caller's value:
1), passing parameters.
2), use static fields to simulate global variables.
If the caller wants to get the value of the callee:
1), return value
C # has no global variables and is implemented with static (class scope).
2.
Whether it is an argument or a formal parameter, it opens up space in memory.
3, the method must be a single function.
Getmax (int n1,int n2)
The most taboo in the method is the word that prompts the user to enter.
4, out, ref, params
1), out parameters.
If you are returning multiple values of the same type in a method, you might consider returning an array.
However, if you return a number of different types of values, the return array will not be, then this time,
We can consider using out parameters. The out parameter focuses on the ability to return several different types of values in a method.
Note: This return is not returned by return, but is brought out by parameters.
For example, login: Returns the login result (success or failure) and login information (user name or password error).
MSG is a normal string outside of the method and does not need to be initialized.
///<summary>// Determine if the login is successful///</summary>//<param name= "name" > Username </param>//<param name= "pwd" > Password </param>//<param name= "MSG" > Extra login Information </param>//<returns> return login Results </retur ns> public static bool IsLogin (string name, string pwd, out string msg) {if (name = = "Admin") && pwd = = "888888") {msg = "login Successful"; return true; } else if (name = = "Admin") {msg = "bad password"; return false; } else if (pwd = = "888888") {msg = "user name error"; return false; } else {msg = "Unknown error"; return false; } }
Some students mentioned that they could return with structure .... It feels right.
2), ref parameter
The ability to take a variable into a method to change, after the change is completed, then the change in the value of the method.
The ref parameter requires that a value be assigned outside the method, and the method may not be assigned a value.
Note: It feels like a value in C is passed. Just don't return the value and change it directly through ref.
For example: Swap variables (without a method like address delivery)
3), params variable parameter
The elements of the argument list that are consistent with the variable parameter array type are treated as elements of the array.
The params variable parameter must be the last element in the formal parameter list.
Note: The last one does not know if the element behind the variable parameter is a variable parameter content; there cannot be more than one variable parameter, so the last variable parameter does not conform to the syntax.
5. Overloading of methods
Concept: Overloading of methods means that the name of the method is the same, but the parameters are different.
Different parameters, divided into two kinds of situations
1), if the number of arguments is the same, then the type of the parameter cannot be the same.
2), if the parameters of the same type, then the number of parameters can not be the same.
The overloads of the method are not related to the return value.
6. Recursion of the method
Method itself to invoke itself.
Find all the files in a folder.
Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; Namespace _14_ method Recursive {class program { static void Main (string[] args) { tellstory (); Console.readkey (); } public static int i = 0; public static void Tellstory () { //int i = 0; write this word dead loop Console.WriteLine ("Formerly there was a mountain"); Console.WriteLine ("A Temple in the mountains"); Console.WriteLine ("There is an old monk and a little monk in the Temple"); Console.WriteLine ("One day, the Little monk cried, the old Monk told the Little Monk a Story"); i++; if (i >=) { //break is jumping out of this loop return; } Tellstory (0); and the first line writes int i=0 an effect tellstory ();}}
C # Learning Notes (iii)