Pass reference type parameters (C #)

Source: Internet
Author: User
Msdn:

Variables of the reference type do not directly contain their data; they contain references to their data. When a parameter of the reference type is passed through a value, it is possible to change the data pointed to by the reference, such as the value of a certain type of member. However, you cannot change the reference value. That is, you cannot use the same reference to allocate memory for the new class and keep it out of the block. To do this, use the ref or out keyword to pass the parameter. For simplicity, the following example usesRef.

The following example demonstrates how to useChangeParameters of the reference type passed by the methodArr. Because this parameter isArrSo it is possible to change the value of the array element. However, if you try to allocate parameters to different memory locations, this operation is only valid in the method and does not affect the original variables.Arr.

 

Class passingrefbyval
{
Static void change (INT [] parray)
{
Parray [0] = 888; // This change affects the original element.
Parray = new int [5] {-3,-1,-2,-3,-4}; // This change is local.
System. Console. writeline ("inside the method, the first element is: {0}", parray [0]);
}

Static void main ()
{
Int [] arr = {1, 4, 5 };
System. Console. writeline ("Inside main, before calling the method, the first element is: {0}", arr [0]);

Change (ARR );
System. Console. writeline ("Inside main, after calling the method, the first element is: {0}", arr [0]);
}
}

 

Output

  Inside main, before calling the method, the first element is: 1

Inside the method, the first element is:-3

Inside main, after calling the method, the first element is: 888

 

Code Discussion

In the previous example, the array ArrIs a reference type. RefParameter. In this case Arr. The output display method may change the content of the array element. In this case 1Change 888. However ChangeUse the new operator to allocate the new memory. ParrayReference a new array. Therefore, any subsequent changes will not affect the original array. Arr(It is in Main). In this example, two arrays are created. MainIn ChangeMethod. Reference address: http://msdn.microsoft.com/zh-cn/library/s6938f28 (V = vs.80). aspx

Example:

 

Protected void button#click (Object sender, eventargs E)
{
Class1 obj1 = new class1 ();
Obj1.name = "name1 ";
Obj1.id = "1 ";

Class1 obj2 = getobj (obj1 );

Response. Write ("obj1 => ID:" + obj1.id + "; Name:" + obj1.name );
Response. Write ("<br/> ");
Response. Write ("obj2 => ID:" + obj2.id + "; Name:" + obj2.name );
}

Protected class1 getobj (class1 C)
{
C. ID = "3 ";

Class1 B = new class1 ();
B. ID = "2 ";
B. Name = "name2 ";
C = B;

Return C;
}

 

Output result:

 

Obj1 => ID: 3; Name: name1
Obj2 => ID: 2; Name: name2

 

Personal Understanding:

 

When an object is passed as a parameter, the object is referenced (that is, the address is used ). For example, if the original object (obj1) is at address 22, then C is first modified in the getobj method. The modified object is at address 22, that is, the obj1 object. When the statement C = B; is executed, the pointer originally directed to the 22 address is assigned a new address, for example, 33, then various operations are performed on C, then, the operation target can only be a new object at address 33, and there is no relationship with the object obj1 at address 22. Therefore, the above results are displayed.

It is extended and inferred that C # transmits parameters by value without adding out and ref when passing method parameters. For a value type parameter, the copied value itself is used. For a referenced object, the object is also copied, but the pointer of the object is copied!

When an out or ref clause is added, the system will re-allocate the original object once there is a memory re-allocation statement such as new in the method, that is, the original object pointer will point to the new object address!

 

If you do not understand it properly, please note that.

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.