Out and ref in C #

Source: Internet
Author: User
C # In the out and ref2007-09-19

A Brief Introduction on msdn:

The out keyword causes the parameter to be passed through reference. This is similar to the ref keyword, except that the ref requires that the variables must be initialized before being passed. To use the out parameter, the out keyword must be explicitly used for method definition and call. Example: Class outexample
{
Static void method (Out int I)
{
I = 44;
}
Static void main ()
{
Int value;
Method (out value );
// Value is now 44
}

Although the variables passed as the out parameter do not need to be initialized before being passed, you need to call a method to assign values before the method returns.

The ref and out keywords are processed in different ways at runtime, but are processed in the same way at compilation. Therefore, if one method uses the ref parameter while the other method uses the out parameter, the two methods cannot be reloaded. However, if one method uses the ref or out parameter, and the other method does not use the two parameters, you can perform the overload.

Example:

When you want the method to return multiple values, it is useful to declare the out method. The method using the out parameter can still use the variable as the return type (see Return), but it can also return one or more objects as the out parameter to the call method. In this example, three variables are returned in a method call using out. Note that the value assigned by the third parameter is null. This allows methods to return values selectively.

Class outreturnexample
{
Static void method (Out int I, out string S1, out string S2)
{
I = 44;
S1 = "I 've been returned ";
S2 = NULL;
}
Static void main ()
{
Int value;
String str1, str2;
Method (out value, out str1, out str2 );
// Value is now 44
// Str1 is now "I 've been returned"
// Str2 is (still) NULL;
}
}

Other discussions

1.When the parameter is of the value type, there are two differences between out and ref.

1. Clear semantics: out must be assigned a value in the called function. The syntax check can ensure that this variable is indeed modified by the function.
2. Relevance: The value assignment before the out function is called is ignored. That is to say, it has nothing to do with the value before the method call. Therefore, you only need to declare it.

My own confusion: When the parameter is of the object type, the ref and out declarations are not used. What is passed to the method is actually the reference of the object, and the object is also modified inside the method, at this time, I am very confused about the functions of ref and out.

Summoner answers !~

2.If a new memory (I .e. re-instantiation) is not opened for parameters in the function body, the out or ref is the same (except for the string class, operations of the string class are more like value types). If you want to re-instantiate a parameter within the function, you need to use ref or out, because if you do not need ref or out, the parameter is actually uploaded as a copy of the Object Reference (just like the true parameter of a ship in C language is actually a copy of The passed pointer). The operation on the original object can be modified, if you want to create a new instance, only this copy points to this memory, and the original object reference still points to the original memory space. Besides, the lifetime of the parameter is only in the function body, and the memory allocated by the function should also be recycled.

3.What I said upstairs makes sense. For example:
Public class clstest
{
Public int intx;

Public clstest ()
{
Intx = 0;
}
}

Static void func (clstest argclstest)
{
Argclstest = new clstest ();
Argclstest. intx = 7456;
}

Static void main (string [] ARGs)
{
Clstest m_cls = new clstest ();

Func (m_cls );

Console. writeline (m_cls.intx );
}
The returned result is 0. If func uses the ref keyword, the result is 7456.

As for "out is more reliable than the program written with Ref", this statement is incorrect. Both out and ref indicate that the parameter uses the keyword passed by reference, they are used together to make the parameter usage clearer: ref, must be initialized before the method is used out; out, must be initialized in the call method. For example, you don't have to. You're very smart.
4.Ref is a reference parameter. The reference parameter does not create a new storage location. The value of the reference parameter is the same as that of the basic variable.

The out parameter is the output parameter, which is in the same position as the variable used as an "independent variable" in the function member call,
The value of the parameter is the same as that of the base variable.

There is a difference between the two. The assignment above is a part.
There are:
Within the function member, the reference parameter is assigned as the initial value, and the output parameter is regarded as the initial value not assigned.

(2) The reference parameter must be explicitly assigned a value before calling the function, and the output parameter must be explicitly assigned a value before returning the function.

 

This article from: http://hi.baidu.com/big_m/blog/item/f36dbf8b3d408bd0fd1f109d.html

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.