C # out and ref difference

C # inside out and ref parameters often used record it:

 

void Start public ()
{
// outSum not necessary assignment, the assignment is completely useless.
// If the internal parameter function directly out AddByOut corresponding, being given: not assigned using out parameter A
int = outSum. 1;
int OV1 = 2;
int OV2 of =. 3;
AddByOut (outSum out, OV1, OV2 of);
Console.WriteLine ( "outSum:" + outSum + "v1:" + ov1 + "v2:" + ov2);


// refSum must be assigned
int = Refsum. 1;
int RV1 = 2;
int = RV2. 3;
// If no value Refsum, being given here: the use of local variables unassigned Refsum
AddByRef (REF Refsum, RV1, RV2);
Console .WriteLine ( "Refsum:" + + Refsum "V1:" + RV1 + "V2:" + RV2);
}

void AddByOut public (out int a, int B, int C)
{
// a = a + B + C; // a not assigned, can not be directly used, even where the corresponding parameter initialization out useless call
a B + C =;
}

void AddByRef public (REF A int, int B, int C)
{
A = A + B + C; // may be used as A
}

 

 

Summary: 1, the function call with the ref keyword must be assigned to the corresponding parameter ref; and out keyword is not required.

    2, with internal function may be used as keywords ref ref corresponding parameters; out function key, it is necessary to use out of the corresponding parameter assignment within the function.

      (Attributed to 1,2 points: ref initialization outside, out inside the initialization)

    3, two keywords can make a value type by using reference types that modify the function of external variables declared

 

Guess you like

Origin www.cnblogs.com/struggle-cs/p/11350812.html