The original address of GSKCC the difference between out and ref in C #
First: Both are passed by address and will change the value of the original parameter after use.
Second: Ref can pass the value of the parameter into the function, but out is to empty the parameters, that is, you can not pass a value from out into the, out into the value of the parameter is empty, so you have to initialize it once. This is the difference of two, or as some netizens say, ref is in there, out is only out.
Ref(C # Reference)
The REF keyword enables parameters to be passed by reference. The effect is that when control is passed back to the calling method, any changes to the parameters in the method are reflected in the variable. To use the ref parameter, both the method definition and the calling method must explicitly use the ref keyword.
For example:
Class refexample{ static void Method (ref int i) { i =; } static void Main () { int val = 0; Method (ref val); Val is now,} }
the arguments passed to the ref parameter must be initialized first. This differs from out, whose arguments do not need to be explicitly initialized before being passed.
Although ref and out is handled differently at run time, but is handled the same way at compile time. Therefore, if a method takes ref
class cs0663_example{ // Compiler error CS0663: "Cannot define overloaded // methods that differ only on ref and out ". public void SampleMethod (ref i) {} public void SampleMethod ( Span style= "color: #0000ff;" >out int i) {}}
However, if a method takes a ref or out parameter and the other method does not take both parameters, it can be overloaded, as shown in the following example:
class refoutoverloadexample{ publicvoid samplemethod (int i) {} Public void SampleMethod (refint
out (C # Reference)
The out keyword causes parameters to be passed by reference. This is similar to the REF keyword, except that the ref requires that the variable be initialized before it is passed. To use an out parameter, both the method definition and the calling method must explicitly use the Out keyword.
For example:
class outexample{ staticvoid Method (outint i) { - ; } Static void Main () { int value; Method (out value); // value is now }}
Although a variable passed as an out parameter does not have to be initialized before it is passed, the method needs to be called to assign a value before the method returns.
The ref and out keywords are handled differently at run time, but are handled the same way at compile time. Therefore, if one method takes a ref parameter and the other method takes an out parameter, the two methods cannot be overloaded. For example, from a compilation point of view, the two methods in the following code are identical, so the following code will not be compiled:
class cs0663_example{ // Compiler error CS0663: "Cannot define overloaded // Methods that differ only on ref and out ". Public void SampleMethod (outint i) {} publicvoid SampleMethod ( ref int i) {}}
However, if a method takes a ref or out parameter and the other method does not take these two types of arguments, it can be overloaded as follows:
class refoutoverloadexample{ publicvoid samplemethod (int i) {} publicvoid SampleMethod (outint i) {}}
[Go] The difference between out and ref in C #