C ා value parameter / reference parameter / output parameter

Time:2020-11-22

Value parameter

1. Allocate space for formal parameters in the stack

2. Copy arguments to formal parameters

 1       public class Person
 2       {
 3           public int age = 10;
 4       }
 5          static void Main(string[] args)
 6         {
 7             int a2= 10;
 8             Person a1= new Person();
 9 
10             Console.WriteLine(a1.age + "--" + a2);
11             SetParam(a1, a2);
12             Console.WriteLine(a1.age + "--" + a2);
13             Console.ReadLine();
14         }
15         static void SetParam(Person f1, int f2)
16         {
17             f1.age = f1.age + 5;
18             f2 = f2 + 5;
19         }     

 

The output above is

10–5

15–5

reference parameter

1. When you use a reference parameter, you must use the ref modifier in both the declaration and invocation of the method.

2. An argument must be a variable and must be assigned a value before it can be used as an argument. If it is a variable of reference type, it can be assigned a reference or a null value.

3. Do not allocate new memory for formal parameters in the stack.

4. The parameter name is equivalent to the alias of the argument variable and refers to the same memory location as the argument.

 1         public class Person
 2         {
 3             public int age = 10;
 4         }
 5         static void Main(string[] args)
 6         {
 7             int a2 = 5;
 8             Person a1 = new Person();
 9 
10             Console.WriteLine(a1.age + "--" + a2);
11             SetParam(ref a1,ref  a2);
12             Console.WriteLine(a1.age + "--" + a2);
13       
14             Console.ReadLine();
15 
16 
17         }
18 
19         static void SetParam(ref Person f1,ref int f2)
20         {
21             f1.age = f1.age + 5;
22             f2 = f2 + 5;
23         }

The output above is

10–5

15–10

Output parameters

1. Modifiers must be used in all declared calls. The modifier for the output parameter is out, not Ref.

2. Arguments cannot be variables or other expression types.

3. Inside a method, the output parameter must be assigned a value before it can be read. This means that the initial value of the parameter is irrelevant and there is no need to assign a value to the argument before the method call.

4. Each output parameter must be assigned a value before the method returns

 1         public class Person
 2         {
 3             public int age = 10;
 4         }
 5         static void Main(string[] args)
 6         {
 7             Person a1 = null;
 8             int a2;
 9 
10             OutParam(out a1,out  a2);
11             Console.WriteLine(a1.age + "--" + a2);
12             Console.ReadLine();
13         }
14 
15         static void OutParam(out Person f1, out int f2)
16         {
17             f1 = new Person();
18             f1.age = 25;
19             f2 = 15;
20         }

 

The output above is

25–15

 

 

Recommended Today

Interviewer: young man, what do you think of the principle of distributed system

1 Concept 1.1 model 1.2 copies 1.3 indicators for measuring distributed systems 2. Principle of distributed system 2.1 data distribution 2.2 basic copy agreement 2.3 lease mechanism 2.4 quorum mechanism 2.5 log technology 2.6 two phase submission protocol 2.7 MVCC 2.8 Paxos protocol 2.9 CAP 1 Concept 1.1 model node In a specific project, a […]