Basic: Pass the reference type by value, and pass the new details of the reference class by reference.

Source: Internet
Author: User

There are many questions about this.ProgramThey do not understand and are often used in routine programming. For example, I declared a variable in the external body of the method, and then I thought of a change in the body of the method.

This value is then used in another method. Of course, in my previous articleArticleAs mentioned in "Basic: Out, Params, ref, and precipitation", you can use ref or out to implement it, but this was not summarized at the time. This method is not changed to a reference transfer reference type.

Next we will describe them separately:

First define such a person class

 
1:# RegionPass a person class defined by reference type by value
 
2:Public ClassPerson {
 
3:Private String_ Personname;
 
4:Private Int_ Personage;
5: 
 
6:# RegionConstructors
 
7:PublicPerson (StringName,IntAge ){
 
8:This. Personage = age;
 
9:This. Personname = Name;
 
10:}
 
11:PublicPerson (){
 
12:}
13:# Endregion
 
14: 
 
15:# RegionProperties
 
16:Public StringPersonname {
 
17:Get {Return_ Personname ;}
 
18:Set {_ personname =Value;}
 
19:}
 
20:Public IntPersonage {
21:Get {Return_ Personage ;}
 
22:Set {_ personage =Value;}
 
23:}
 
24:# Endregion
 
25: 
 
26:Public VoidDisplay (){
 
27:Console. writeline ("Name: {0}, age: {1 }",This. Personname,This. Personage );
 
28:}
29:}
 
30:# Endregion
 
 

Create a method to pass the object of the person class as a value type.

1: Static VoidSendapersonbyvalue (person p ){
 
2:// Change the age of P
 
3:P. personage = 99;
 
4:// Can the caller see the re-constructed object in the method body?
 
5:P =NewPerson ("Yeanjay", 24 );
 
6:}
 
 

In this way, we can call this method in main as follows:

 

 
1:// Pass the reference type by value
 
2:Person P =NewPerson ("Ken", 23 );
3:Console. writeline ("Before calling the method :");
 
4:P. Display ();
 
5:Sendapersonbyvalue (P );
 
6:Console. writeline ("After the method is called :");
 
7:P. Display ();

 

The execution result is as follows:

It can be seen that the personage attribute is changed, and it seems that the semantics of "by value" is violated. However, the reference is passed in. During the copy process, the reference pointing to the caller's object is copied.

When the sendapersonbyvalue () method is called and the caller points to the same object, the status data can be changed. However, in the called method body, re-instantiation is not allowed.

Next, let's take a look at the reference type passing by reference:

Similarly, the above class is used to define the following call method:

1:# RegionTransmit reference types by reference
 
2:Static VoidSendapersonbyref (RefPerson p ){
 
3:P. personage = 27;
 
4:P =NewPerson ("Yeanjay", 24 );
 
5: 
 
6:}
 
7:# Endregion

In this way, change sendapersonbyvalue (p) in main to sendapersonbyref (RefP );

The call result is as follows:

Obviously, after using ref, pass the reference type according to the reference. In the method, you can change the point of the passed reference in the memory.

Here are the prime rules for transferring reference types by reference in pro C #2008 and. Net 3.5 platform in Andrew troelsen:

1. If the reference type is passed by reference, the caller may change the state value of the object and the referenced object;

If a reference type is passed by reference, the callee may change the values of the object's
State data as well as the object it is referencing.

2. If the reference type is passed by value, the caller may change the value of the object's State data but cannot change the referenced object.

If a reference type is passed by value, the callee may change the values of the object's state
Data but not the object it is referencing.

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.