Analysis of n cases of B = A in C #

Source: Internet
Author: User

This article aims to verify a confusing concept and pave the way for inotifypropertychanged and observablecollection in WPF/Silverlight.

Two variables A and B of the same type have the following relationship:

B = A;

If a changes, does B change?

The situation is very complicated. It can be discussed in the following situations:

1) single entity

1. Simple Type

First test INT:

Int A =   1 ;
Int B = A;

A= 2;
Console. writeline ("B:" +B );

Output result:

Check the string:

String A =   " 1 " ;
String B = A;

A= "2";
Console. writeline ("B:" +B );

Output result:

If you are not at ease, you can test the enum. The result is similar. For details, see the demo.

Conclusion: Simple type is the most basic unit for composite types. It is an atom and cannot be split. Therefore, no matter the value type is double, Int, or reference type string, B does not change with, because they point to the same address on the global stack (for string, it is managed heap.

2. Composite Type

Composite types are simple types such as string, Int, and double.

Define a composite reference type (class) and a composite value type (struct) respectively ).

Class Userinfo
{
Public   String Username;
Public   Int Age;
}

StructUserinfo2
{
Public StringUsername;
Public IntAge;
}

First, we will discuss the reference type:

Userinfo =   New Userinfo () {Username =   " Baobao " , Age =   27 };
Userinfo B = A;

A. Username= "Andresliu";
A. Age= 30;

Console. writeline ("B. Username:" +B. username );
Console. writeline ("B. Age:" +B. Age );

Output result:

Conclusion: B and a still point to the address of the same userinfo instance on the managed stack. The userinfo instance contains the addresses of username and age on the managed heap and global stack respectively. Therefore, to modify the username and age of A, only change the address of the two Members, but not change the address of the userinfo instance. Therefore, the username and age of B members will also change.

Let's partially modify the aboveCode:

Userinfo =   New Userinfo () {Username =   " Baobao " , Age =   27 };

Userinfo B=A;

// A. Username = "andresliu ";
// A. Age = 30;

A =   New Userinfo () {Username =   " Andresliu " , Age =   30 };

Console. writeline ("B. Username:" +B. username );
Console. writeline ("B. Age:" +B. Age );

Output result:

Conclusion: Re-instantiate a, causing a to point to the address of a new userinfo instance. B still points to the address of the original userinfo instance, so B will not change with. Since then, B and A are two variables without any relationship.

Let's take a look at the value type:

Userinfo2 =   New Userinfo2 () {Username =   " Baobao " , Age =   27 };

Userinfo2 B=A;

. username = " andresliu " ;< br>. age = 30 ;

Console. writeline ("B. Username:" +B. username );
Console. writeline ("B. Age:" +B. Age );

Output result:

Conclusion: The problem is concentrated in the sentence B =. B points to a copy of a, pointing to a different address on the global stack. Therefore, B has nothing to do with a, and B does not change with.

2) Set

1. add, delete, and modify a data set.

List < Userinfo > A =   New List < Userinfo > ();

List<Userinfo>B=A;

. add ( New userinfo () {username = " Baobao " , age = 27 });

console. writeline ( " B. count after adding: " + B. count);
console. writeline ();
console. writeline ( " after modifying a [0] " );

A [ 0 ]. username = " andresliu " ;
A [ 0 ]. age = 30 ;

console. writeline ( " B [0]. username: " + B [ 0 ]. username);
console. writeline ( " B [0]. age: " + B [ 0 ]. age);
console. writeline ();

A. Remove ([0]);

Console. writeline ("B. Count after deleting:" +B. Count );

Output result:

Conclusion: B changes with the increase or decrease of data in. Because B and a point to the memory address of the same list <userinfo> instance on the managed stack, this is the same as the composite type.

The array is not mentioned. It can be regarded as a set of multiple variables, so it is processed according to the set. I wrote several pieces of test code and put it in the demo.

Download the sample code: testequal.zip

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.