Use of the parameters in C #

xiaoxiao2021-03-06  54

Use of the parameters in C #

C # has four parameters, namely:

Value parameters: Do not use any keywords

Quote Parameters: Use Keyword REF

Output parameters: Use Keyword OUT

Array parameters: Use Keyword params

1, value parameters

The actual value of the value parameters will not be modified. When the parameter is passed, the actual delivery given is only a copy of the argument, and the type of participation must be consistent. At least the shape is involved in the real-consoles. Type conversion.

2, reference parameters

If you need to maintain the changing dynamics of the active participation, you need to use the reference type. At this time, the address is delivered, and if the real gate value changes, the guanjun also changes accordingly.

The reference type parameter uses the keyword REF, but it is important to pay attention to the passage of the variable as a reference type parameter.

example:

Namespace Test_ref

{

Public Class TestRef

{

Public static int testRefpar (Ref int first)

{

FigSt = 20;

Return 0;

}

Public static void main ()

{

INT i = 10;

System.Console.writeline ("" In not use reference to passing the value of the first i); {0} ", i);

INT J = TestRefpar (Ref i);

System.console.writeline ("When using reference passes, the return value of the member method: {0}", J);

System.console.writeline ("Use the reference to pass, the value {0}", i);

}

}

}

3, output parameters

The output parameter is defined by keyword OUT, which is similar to the reference parameters, no need to open a memory area, and the difference is that before the method call does not need to initialize the argument of the output parameters. The output parameter applies to the case where the member method is transferred back to the data.

Here is an example I wrote:

Namespace Testout

{

Public Class Test_out

{

Public static string test_out_par (Out String Backstring)

{

BACKSTRING = "This is the variable value of the OUT member method being called."

Return "This is the return value of the OUT member method";

}

Public static void main ()

{

String z; // No initialization

System.console.writeline (Test_out_Par (OUT Z)); // The output is the return value of the member method

System.console.writeline (z);

}

}

}

4, array parameters

The number of parameters required can be variable or may be used, and the array parameters are used to define the keyword params. When defining the array parameters, pay attention to the following points:

(1) The array parameters must be the last parameter in the list of method parameters.

(2) The array parameters must be a one-dimensional array.

(3) cannot be used in combination with REF and OUT keywords.

Below is an example:

Namespace Test_Params

{

Public Class TestPar

{

Public Static Int Total (params int [] PAR)

{

INT SUM = 0;

System.console.write ("The number of numbers to calculate: {0}, name:", par.length);

Foreach (int TMP in par)

{

System.console.write ("{0},", TMP);

SUM = TMP;

}

System.console.writeline ("/ N total: {0} / n", sum); Return SUM

}

Public static void

Main

()

{

Int Sum1, SUM2, SUM3, SUM4

Int [] Num = {12, 4, 56, 78, 34};

SUM1 = Total (2, 5, 6, 7, 22);

Sum2 = Total (9, 38, 37, 2, 3, 5, 6);

Sum3 = Total (1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

SUM4 = Total (NUM);

System.console.writeline ("/ N final result: / nsum1 = {0} / nsum2 = {1} / nsum3 = {2} / nsum4 = {3}", SUM1, SUM2, SUM3, SUM4);

}

}

}

转载请注明原文地址:https://www.9cbs.com/read-117144.html

New Post(0)