C # string

Source: Internet
Author: User

String is a special data type, which is both a primitive type and a reference type. during compilation and running ,. net has done some optimization work on it. The formal optimization work will sometimes confuse programmers and make people doubt whether it is really a reference type?

Question 1: The string is constant.

1 string a = "string 01";
2 string b = a;
3 a = "string 02";
4 Console.WriteLine("a : {0}", a);
5 Console.WriteLine("b : {0}", b);
6 Console.WriteLine("a reference equal b is {0}",ReferenceEquals(a, b));

The value of B is constant and does not change with the value of a. a and B do not point to the same memory address, Why?

Question 2: String resident

1 string a = "string 03";
2 string b = "string 03";
3 Console.WriteLine("a reference equal b is {0}",ReferenceEquals(a, b));

Why do two new reference types point to the same memory address?

Question 3: What is the difference between string = and new stirng ()?

1 string a = "x";
2 string b = new string('x',1);
3 Console.WriteLine("a reference equal b is {0}",ReferenceEquals(a, b));

Why does a and B point to the same memory address?

 

To clarify the above questions, we need to first understand how a managed program runs.

When running a managed program, CLR will create a Default AppDomain for this, but in fact Windows does more than that. The reason why an Application is executed in a hosted environment is that the CLR hosts the Application. Therefore, CLR loading is required. However. NET Framework is built on the Windows platform. Windows can be seen as an encapsulation of computer hardware. NET Framework can be seen as an encapsulation of Windows, through. NET Framework API encapsulates the traditional Win32 encapsulation. Because Windows is the infrastructure of the. NET Framework,. NET Framework can only be built in a way that Windows can understand. For Windows, all files that can be loaded and executed are a PE file, such as exe and dll. The same is true for CLR. It is actually implemented in the form of a COM Server in a directory called MSCorWks. Dll that exists in the. NET Framework. When the program starts running, an AppDomain called SystemDomain is created, and SystemDomain loads MSCorEE. Dll..Load the corresponding version of CLR by defining a function named CorBindToRuntimeEx In the DLL and return an unmanaged ICLRRuntimeHost interface. SystemDomain is the hub of the entire Process. It is responsible for creating, initializing, and uninstalling SharedDomain and DefaultDomain.

We know that AppDomain is an Assembly-hosted container. Assembly is generally based on a separate AppDomain and cannot be shared with another AppDomain. However, some assemblies are highly available, such as some primitive types of objects, int, Array, and ValueType that we often use. However, we hope that they can be shared by other AppDomains after being loaded by an AppDomain, this saves a lot of memory space and performance loss caused by Assembly loading. These assemblies are loaded into the SharedDomain. Our common MScorLib. dll is the SharedDomain to be loaded in this way. The Default Domain is the AppDomain created for the specific Application. It is generally named with an executable file name. In DefaultDomain, you can use AppDomain. CreateAppDomain to create another AppDomain. So when we run a managed Application, we actually created three different AppDomains: SystemDomain, ShatedDomain, and DefaultDomain, while SystemDomain and ShatedDomain are based on the entire process, which can be shared by defadomain domain and AppDomain created by it.

With the above foundation, it is not difficult to understand the String resident mechanism.The String resident mechanism is actually carried out in SystemDomain. After the CLR is loaded, a Hash table data structure is created in the managed heap corresponding to SystemDomain. We can call this Hashtable as an Interning table, because it is used to save the resident string, the Key of the Interning table is the string itself, and the Value is the address of the string object.. When our hosting program needs a string, CLR first tries to find the corresponding Item in the Interning table according to the string hash code in this Hashtable. If it is found, the corresponding reference is directly returned. Otherwise, the string is created in the managed heap corresponding to SystemDomain, added to the Interning table, and the reference is returned. Therefore, we say that the string resident is based on the entire process and can be shared across AppDomain.

Q &

  • The uniqueness of String: Once a String is created, its character sequence cannot be changed in the hosting environment. Therefore, we must use Stringbuilder for String concatenation, otherwise, a large number of resident strings are generated.
  • String resident: CLR implements the resident mechanism for the creation of String. CLR only maintains strings with different character sequences. That is, the strings used in the program with identical character sequences correspond to the same String object and are referenced in the memory of the same segment, in addition, the String resident mechanism is not only based on a specific AppDomain, but also on the entire process.
  • The difference between string = and new stirng (): "string =" has different loading times: the "x" in the first line is a constant, it has been placed in a place called a constant pool during the compilation phase. The constant pool usually loads some data identified during the compilation phase, while "new stirng () "is a string object generated by the CLR In the heap at runtime with the value of" x ", so the latter does not reside.

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.