(Zt) Why are there so many strange things? -- Is string a reference type?

Source: Internet
Author: User

(Zt) Why are there so many strange things? -- Is string a reference type?

I,
String is the reference type, so it is the address to compare the two instances.
String S = "hello ";
String T = "hello ";
S, T should not be equal.
II,
Console. writeline (object) 1 = (object) 1); // The result is false.
Console. writeline (object) "OK" = (object) "OK"); // The result is true.
Why? What does the packing value mean?
3,
String A = "hello", B = string. Copy (A), c = "hello ";
Console. writeline (object) A = (object) B); // false
Console. writeline (object) A = (object) C); // true
Why?
IV,
String S, T;
S = "OK ";
T = s;
S = "yes"; // changing S does not change t at the same time
Does this seem to be inconsistent with the reference type conventions? (Similar to the value type)
Why is string unchangeable?
Is the definition of string different from other classes? (For example, stringbuild, do not post the definition of string, just say the reason)
For other classes, if two objects reference the same instance, either of them will change.

Lovingkiss ]:
I,
String is the reference type, so it is the address to compare the two instances.
String S = "hello ";
String T = "hello ";

String is a special reference type ~~ -- Special Handling in special cases

[Fengyunluanwu ]:
Mark

Lovingkiss ]:
Whether string is a value type or a reference type
Of course, string is definitely a reference type, but this special class shows the value type characteristics:
When determining equality, it is determined by the content, not the address.
It must be a reference type. There are two aspects:

1. Class string inherits from the object, rather than system. valuetype (int32 inherits from system. valuetype)
2. String is essentially a char [], while array is a reference type, and memory is allocated during initialization.
When Microsoft designed this class, it was estimated that it was easy to operate. Therefore, the operator and equals method were rewritten. Otherwise, we would judge that the string is equal to the following:
Foreach (char C in S. tochararray ()){...}
But another common object, Microsoft, does not help rewrite the equivalent Judgment Method: Array
In this way, int [] A = {1, 2, 3} and int B = {1, 2, 3}, A = B? // False

Another mistake is whether to pass by reference or by value:
The reference type is passed by reference, and the value type is passed by value, all of which are good.
A reference type, such as the system. array class, transmits a pointer when it is passed as a parameter to a method. Code Is it equivalent?
Void test (array A) and void test (ref array)
The result is not completely equivalent.
If the constructor calls the constructor internally to create an object and assign parameters to it, the variables outside the function will not be affected;
For example, a = new...
If you only modify the field of this parameter (an object), it will be affected. In this case, the addition of ref is equivalent.
For example, a [I] =...
Another special feature of the string type is that it does not change. Each string operation creates a new String object. Therefore, for the string type, void test (string S) it is always different from void test (ref string S. Here, the string type shows the value type again. We think this is a value transfer-actually transmitted or an address, but the string is initialized again during the operation, and this change cannot be obtained externally.
For the concept of variable scope, Microsoft's design is also reasonable: since it is an object built inside the function, the external should not be able to access this object. After the function is completed, these objects will be collected by GC and will not affect the external Program .
----------
Update on [23, August]
String can be considered as the packaging of char [], but in fact CLR is not implemented using hosted char:
Here is the code about the string indexer:
Public char this [int Index]
{
Get
{
Return this. internalgetchar (INDEX );
}
}
The internalgetchar function is:
[Methodimpl (methodimploptions. internalcall)]
Internal char internalgetchar (INT index );
Here we can see that string is implemented by calling the CLR method internally, rather than the packaging of char [].

Lovingkiss ]:
III,
Dim s as string, B as string
S = "ABCD"
B = string. Copy (s)
If S = B then
Msgbox ("Equal") 'indicates equal
Else
Msgbox ("not equal ")
End if
I don't understand C #. Have you finally converted it into an object ??

Lovingkiss ]:
4. String is indeed a reference type with the Value Type feature. Does it seem unnecessary?
Do you really need reasons ?? -- Why does Microsoft do this ??
You can consider asking why Microsoft wants to make it easier-to make string a nondescribable

[Lizhizhe2000 ]:
1. String is overloaded with some operators.
2. String is an immutable object, so the stringbuilder class is displayed. The upstairs brother has already understood it!

[Amandag ]:
Get up so early to learn, praise first...

I,
String is the reference type, so it is the address to compare the two instances.
String S = "hello ";
String T = "hello ";
S, T should not be equal.
========================================================== ======
Each string is a string object, but. Net has the concept of a string pool. If the same string is used, it will be searched from the string pool instead of a new string. (If you view the Il command, you will find that there is no newobj command, but a special il command loads a string ), this is because CLR has a more special and efficient way to construct string objects.
Therefore, there is only one "hello" in the heap, and S and T both point to the reference of this string. They are of course equal.

II,
Console. writeline (object) 1 = (object) 1); // The result is false.
Console. writeline (object) "OK" = (object) "OK"); // The result is true.
Why? What does the packing value mean?
========================================================== ======================
This problem is similar to the previous one. After packing (object) 1 on the left and right has a new address in the heap, but the string is not like this.

3,
String A = "hello", B = string. Copy (A), c = "hello ";
Console. writeline (object) A = (object) B); // false
Console. writeline (object) A = (object) C); // true
========================================================== ======================================
Similar to the first one, only the copy Heap has a new address.

IV,
String S, T;
S = "OK ";
T = s;
S = "yes"; // changing S does not change t at the same time
Does this seem to be inconsistent with the reference type conventions? (Similar to the value type)
Why is string unchangeable?
Is the definition of string different from other classes? (For example, stringbuild, do not post the definition of string, just say the reason)
For other classes, if two objects reference the same instance, either of them will change.
========================================================== ======================================
Although the string is a reference type, it does look like some value types and the details of the reasons why it cannot be changed,
1. We can perform various operations on a string, but the string does not change.
2. There will be no thread synchronization issues during string operations

However, the cost for this performance improvement and direct access is that the string must be of the sealed type. Because CLR does not want us to add our own fields to destroy it.

If you still don't know, I suggest you refer to the. NET Framework Program Design book.

[Bote_china ]:
1. Although string is a reference type, the equal operators (= and! =) Is defined to compare the "value" of a string object (instead of a reference"
2. (object) 1 = (object) 1 outputs false. This is because int is a value type, and the predefined equal reference type operator has no significance for the value type. Forced conversion creates references for two separate instances with boxed int values.
3. variables A and B reference two different string instances with the same character.

Lovingkiss ]:
I am skeptical about amandag.

If S and T both point to the reference of this string, they are of course equal -- but if I change the content of S at this time, it is equivalent to changing the space he references ?? Then I changed T, and T's space also changed? Is the string buffer without variables recycled?

Well, I have no theoretical basis, but I feel
1. String comparison is provided by Microsoft. It compares the content rather than the address. -- This is for sure.
2. Should the storage of string space depend on variable declaration? It should have been the space defined at the time of declaration. -- This is the feeling. Otherwise, when I frequently change a string, isn't it necessary to change the space where the variable is located every time ??

Lovingkiss ]:
String S = "helloabc ";
String T = "hello ";

If the string is buffered, will the s space also include t space ?~~ Haha ~~

Lovingkiss ]:
Another special feature of the string type is that it will not be changed. Each string operation is equivalent to creating a new String object. Therefore, for the string type, void test (string S) it is always different from void test (ref string S ).

[Twtqing ]:
The people upstairs are senior learners.

[Justlovepro ]:
Mark

[Wzd24 ]:
Agree with Gao ge.

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.