简体   繁体   中英

what's means of value type and referance type in .net

您好,我有点困惑valu类型或引用类型...例如,int是一个值类型字符串是引用类型,但是int也引用了一些值,例如int int i=4 int j=i这里j也被引用为4就像我为什么不将其称为引用类型...在string s="hello" string s1=s s是引用s .....任何人都可以从存储的角度进行解释。 .....对值类型或引用类型是否有任何限制。

int i=4;
int j=i;

int is value type, it means that this expression j=i copies value from i to j.

string s="hello";
string s1=s;

string is reference type so here not value is copied but reference to that string, so they now point to same object.

A value type holds a value, a reference type holds a reference to another variable.

In your example, i holds the value 4, but j=i means j still holds the value 4, even if i changes to 5. j doesn't change it's value, so it's not a reference type.

Strings are the same way. You can say s1 = s, but when s changes to 'hello2,' s1 stays 'hello'. Acts like a value type, but under the hood, stored like a reference type, see below.

Arrays and objects are reference types. If you say obj2 = obj, and obj changes, obj2 also changes. Obj2 holds a reference, or pointer to, obj, changes and all.

also see: Visual Basic .NET Syntax Reference Primer

containing:

A string is initialized as follows:

Dim myString as String = "Hello, World!"

Strings are Immutable, meaning once you assign a value to them they cannot be changed. Whenever you assign another value to a string or concatenate a value to a string, you are actually creating a new copy of a string variable and deleting the old copy of the string. (This has potential performance considerations if you are doing repeated work on a string variable, use the StringBuilder for such tasks as it is not immutable ie. The StringBuilder is Mutable).

I think it's more accurate to say you're deleting the old reference to the string variable, as any other strings pointing to it retain their value.

Ask yourself what happens when things change:

int i = 4;
int j = i;

i = 7;  // Q1). now what value does j have?

j = 99; // Q2) what value does i now have?

Q1). assigning 7 to i has no effect on j, j still has the value 4.

Q2). assigning 99 to j has no effect on i, i still has the (latest) value 7;

Strings are a bit tricker to explain, because in some languages you can't change them. So I'll use an (invented) language where it's legal to change values in a string.

string s1 = "Hello";
si[0] = 'J';  // now s1 is set to "Jello"

So we say

string s1 = "wibble";
string s2 = s1;
// here s2 is **referring** to "wibble", the same "wibble" that s1 refers to.

s1[1] = 'o';  

// now what does s1 refer to? "wobble"

// Big question: what does s2 refer to? The very same thing, "wobble".

So s1 and s2 are references to the same thing at the moment. We could now write

s1 = "SomethingElse";

Now we have chnaged the reference in s1, but s2 still refers to "wobble"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM