. Net reference type string

Source: Internet
Author: User
The definition of string in. NET 2.0 is as follows:

[Serializableattribute] [Comvisibleattribute(True)] Public Sealed Class String:Icomparable,Icloneable,Iconvertible, Icomparable<String>,Ienumerable<String>,Ienumerable, Iequatable<String>


There is no doubt that string is a reference type, but it also shows some value type characteristics during use.
1. The string has been overloaded with = ,! = Operator

// Heavy Load = Operator Public Static Bool Operator= (StringA,StringB) { ReturnEquals (A, B ); } // Heavy Load ! = Operator Public Static Bool Operator! = (StringA,StringB) { Return! Equals (A, B ); }


The string object overrides the equals method to compare strings.

2. String is read-only.
The value of this object instance cannot be modified. The modification to this object instance in actual operations returns a new instance of this object. Stringbuilder is modified on the basis of the original object instance. This is why stringbuilder is more efficient than the string object during the modification operation. The string object is retained on the stack, rather than on the stack. When the same string is assigned to two string variables, two references of the strings in the same memory are obtained. This aims to improve the efficiency, it is optimized specially. strings are commonly used basic data types and are read-only. It is not necessary to keep multiple copies of the same string in the memory.
// The same string is assigned to two values. String Variable, two references of the same string in the memory will be obtained. String AA ="Hello"; String BB ="Hello"; Console . Write (String. Equals (AA, BB ));// True Console . Write (String. Referenceequals (AA, BB ));// True // Change the string to get a new instance BB + ="O"; Console . Write (String. Equals (AA, BB ));// False Console . Write (String. Referenceequals (AA, BB ));// False

3. Illusion in parameter transfer
Question: When a string variable is passed as a parameter, its value is not changed.
String STR ="Hello"; Console . Writeline (STR );// Hello Console . Writeline (changestring (STR ));// Helloo Console . Writeline (STR );// Hello Console . Writeline (String. Equals (STR, STR ));// True Console . Writeline (String. Equals (STR, changestring (STR )));// False // Change string Private StringChangestring (StringStr) { STR + ="O"; ReturnSTR; }

The reason is that the string is read-only and a new object is created when it is changed. In this case, the ref or out should be clearly identified.
This is not an individual phenomenon. When passing the reference type as a parameter, you should specify the ref or out when you want to change the object, this reminds me of why the original pointer itself was not changed when the C language was used as a pointer for parameter transmission: the pointer address should be passed.
The followingCodeThe example is obvious:

// Test Test1 Tst =New Test1(); Tst. AA = 1; Changeclass (TST ); Console . Writeline (TST. aa );// 1 Changeclassref (RefTst ); Console . Writeline (TST. aa );// 3 // Test Value Transfer Parameters Public VoidChangeclass (Test1Tst) { Tst =New Test1(); Tst. AA = 2; } // Test reference Transfer Parameters Public VoidChangeclassref (Ref Test1Tst) { Tst =New Test1(); Tst. AA = 3; } // Test class Public Class Test1 { Public IntAA = 0; }

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.