The difference between the original ASP.net out and ref

ref and out are C # keywords, implemented functions are similar specify a parameter passed by reference.
For the compiled program, there is no difference between them, that they only grammatical difference.
To sum up, they have differences in syntax is as follows:

1, ref pass in parameters must be initialized before the call, out need not, i.e.:
int I;
SomeMethod (I REF); // syntax error
// by; SomeMethod (out i)
inside the function 2, ref pass in parameters It can be used directly, and not out:
public void SomeMethod (int I REF)
{
int J = I; by //
// ...
}
public void SomeMethod (int I out)
{
int J = I; // syntax error
}
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
. 3, REF pass in parameters may not be modified within the function, but it must be assigned out before leaving the body of the function.

ref must be initialized before transmission parameters; and out is necessary to initialize before transmission, and the process of converting the value type and reference type ... called boxing and unboxing.

Summary:
It should be said, the system limits on ref is less some. Although not required out must be initialized prior to the call, but its value is not visible inside the function, that is, not use the value passed in through out, and be sure to assign a value within the function. Or liable to initialize the variable function.
Here to talk about ref and out in the end what is the difference:
a heavy load on
the principle: there are out | ref keyword method may constitute overloaded methods with no ref and out keywords; but if want to override between out and ref, the compiler will prompt: heavy-duty can not be defined only on the method of ref and out

2 on the initial value before calling
principle: ref as a function of the parameters before calling, arguments must be assigned an initial value. Otherwise, the compiler will Hint: Use a local variable unassigned;
OUT parameter of the function before calling, (Today's headlines Search: White ah no food) argument can not be assigned an initial value.

3 initial parameters in regard to function, introduced (Today's headlines Search: White ah no dishes)
principle: in the called function, parameter assignment out introducing at least once before returning, otherwise the compiler will Hint: Use the out parameters unassigned;
in the called function, parameter introduced ref does not have its initial value before returning.

Summary: C # ref and out of providing value types are passed by reference solution,
of course, a reference type can also be modified by ref and out, but it has lost its meaning.
Because the reference data types that have been copied itself, not passing a reference value. ref and out keyword tells the compiler that is now passing parameters address instead of the parameters itself, which is a reference type and the default transfer mode is the same. Meanwhile, the compiler does not allow overload configuration between out and ref, and the difference between out and ref fully explained merely compiler angle, they generate the same IL code.
One might doubt, and when I began to learn the same doubts: Value types do not allocate memory in the managed heap, and why it can be passed by address? Although value types live in the thread's stack, which itself is represented by the data itself (which is different from the reference data type itself does not mean that data but points to a memory reference), but the value type has its own address, namely pointers, now with after ref and out modifications, the pointer is passed, it can be a, b and after modifying the actual exchange achieved. This is the ref and out the benefits brought to us.

First: both are passed by address, after use will change the value of the original parameters.
Second: rel value of the parameter can be passed into the function, but is put out parameter empty, you can not put a value that is passed from the inside out, and go after out, the value of the parameter is empty, so you must be initialized once. This is the difference between the two, or as some friends say, rel there is a carry out, out is out without.

ref (C # Reference)
REF keyword causes the parameters passed by reference. The effect is that when control passes back to the calling method, any changes in the method parameters are reflected in the variable. To use a ref parameter, the method definition and the calling method must explicitly use the ref keyword. (Today's headlines search: no white dish ah) example:
class RefExample
{
static void Method, (int I REF)
{
I = 44 is;
}
static void the Main ()
{
int Val = 0;
Method, (REF Val);
// Val 44 is now iS
}
}
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
parameters passed to ref parameters must first be initialized. This is different with out which parameters need not be explicitly initialized before transmission.
Despite the different ref and out process at runtime, but the same approach at compile time. Thus, if a method using the ref parameter, the other parameters Methods out, these two methods can not be overloaded. For example, from the viewpoint of the compiler, the following two methods are identical code, and therefore will not compile the following code:

CS0663_Example class
{
// Compiler error CS0663: "CAN Not DEFINE Overloaded
. Differ // Methods that only ON and REF OUT"
public void SampleMethod (int I REF) {}
public void SampleMethod (I OUT int) {}
}
. 1
2
. 3
. 4
. 5
. 6
. 7
, however, if a method using the ref or out parameters, and another method without using these two parameters may be overloaded in the following example:

RefOutOverloadExample class
{
public void SampleMethod (I int) {}
public void SampleMethod (int I REF) {}
}
. 1
2
. 3
. 4
. 5
OUT (C # Reference)
OUT keyword cause parameter passed by reference. This is similar to the ref keyword, except that ref requires variable must be initialized before delivery. To use out parameters, method definition and the calling method must explicitly use the out keyword.

For example:
class OutExample
{
static void Method, (I OUT int)
{
I = 44 is;
}
static void the Main ()
{
int value;
Method, (OUT value);
// value 44 is now IS
}
}
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
to be initialized before although variable passed as an out parameter need not be transmitted, but the need to invoke a method before the method returns to the assignment.
ref and out keywords in handling runtime different, but the same approach at compile time. Thus, if a method using the ref parameter, the other parameters Methods out, these two methods can not be overloaded. For example, from the viewpoint of the compiler, the following two methods are identical code, and therefore will not compile the following code:

CS0663_Example class
{
// Compiler error CS0663: "Can Not DEFINE Overloaded
. Differ // Methods that only ON and REF OUT"
public void SampleMethod (I OUT int) {}
public void SampleMethod (int I REF) {}
}
However, if a methods ref or out parameters, and another method without using these two parameters may be overloaded, as follows:
class RefOutOverloadExample
{
public void SampleMethod (I int) {}
public void SampleMethod (out int I) {}
}
---------------------
author: Zhang Nan Yu Sir
source: CSDN
original: https: //blog.csdn.net/qq_42075398/article/details/84197017
Disclaimer: This article is a blogger original article, reproduced, please attach Bowen link!

Guess you like

Origin www.cnblogs.com/Jeely/p/11058629.html