Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the storage principle of String type in C #

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of the storage principle of String type in c #, the content is detailed and easy to understand, the operation is simple and fast, and has certain reference value. I believe you will gain something after reading this article on the storage principle of String type in c #. Let's take a look at it.

Before we officially understand the String type in c #, let's judge the result of the following code.

String str1 = "123"; String str2 = str1;str2 =" 321"; Console.WriteLine (str1)

The final output of the above code is 123. if you have learned the reference type, you must ask: isn't str2 storing references to str1? So doesn't str2 and str1 point to the same memory space in the heap? Why print out str1 after referencing str2 to change the data and finally print out 123?

This is also my initial question, but don't worry, take a look at it step by step, I believe you can understand it soon.

Before we get started, let's take a look at the memory partitions in c#:

Memory partition

Stack area: automatically allocated and released by the compiler, storing the object of the value type itself, the reference address of the reference type (pointer), the reference address of the static area object (pointer), the reference address of the constant area object (pointer), and so on. It operates in a manner similar to the stack in the data structure.

Heap area (managed heap): used to store the reference type object itself. In c # it is managed by the garbage collection mechanism (GC) of the. Net platform. Stack and heap belong to dynamic storage area, which can be allocated dynamically.

Static area and constant area: used to store static classes, static members (static variables, static methods), constant objects themselves. Because the reference addresses in the existing stack are put into the stack first at the beginning of the program running, the life cycle of the objects in the static zone and constant zone will continue until the end of the program running. at that time, the objects in the static zone and constant zone will be released and recycled (automatically released by the compiler). Therefore, we should limit the use of static classes, static members (static variables, static methods), constants, otherwise the program load is high.

Code area: stores the binary code inside the function.

In c #, the storage mode of String is very special. In the memory of c #, a piece of space is allocated in the constant area called String transient pool (constant pool). In some cases, our string data is stored in this constant pool, while the address is still stored in the stack.

For example, if the String variable is created with String str = "xXXXX", then the value of String will be stored in the String constant pool. When we create the String variable in this way, the compiler will first determine whether your content has already appeared in the constant pool. If so, it will no longer use space in the constant pool to store the same content. This content will only have a fixed reference. So when creating String with the same content, their references are all the same. There is another situation: at the beginning, the content of An and B are the same, that is, when the references of An and B are the same, then the content of B will be changed, then the content of B will use another piece of space in the constant pool, then the reference of B will also change, and the reference of A will not change, because An is still the original content stored at this time. We can look at a simple illustration:

Above we can use the code to confirm our conclusion:

String str1 = "123"; String str2 =" 123"; Console.WriteLine ("the value in str1 has not been changed at this time:"); if (object.ReferenceEquals (str1,str2)) {Console.WriteLine ("same reference at this time") } else {Console.WriteLine ("different references at this time");} if (object.ReferenceEquals (String.Intern (str1), String.Intern (str2)) {Console.WriteLine ("now stored in the same constant pool with the same references") } else {Console.WriteLine ("at this time the two strings are different, exist in different spaces, and the references are also different");} Console.WriteLine (); str1 = "12" Console.WriteLine ("change the value of str1 at this time to compare whether the reference and memory space of str1 and str2 are the same:"); if (object.ReferenceEquals (str1, str2)) {Console.WriteLine ("same reference at this time") } else {Console.WriteLine ("different references at this time");} if (object.ReferenceEquals (String.Intern (str1), String.Intern (str2)) {Console.WriteLine ("now stored in the same constant pool with the same references") } else {Console.WriteLine ("at this time the two strings are different, exist in different spaces, and the references are also different");}

You can see the result of the final run:

To better understand the above code, here are some explanations of the code:

Object.ReferenceEquals

This is used to compare whether the references of the two variables are the same, and if so, true will be returned, otherwise false will be returned.

String.Intern

How String.Intern works is easy to understand. You use this interface with a string as an argument, return a reference to the existence if the string already exists in the pool, add it to the pool if it doesn't exist, and return a reference.

Of course, the above is only about using String str = "XXXXX"; to create variables, so when will the creation of a String consider such a question? Here's a summary of the situation:

We need to know that not all strings are placed in the constant pool:

Store the scratch pool:

Create a String object with a literal value, for example: String str = "ABCD"

Use String.Intern (), for example: StringBuilder sb = new StringBuilder ("ABCD"); string str1 = "ABCD"; string str2=string.Intern (sb.ToString)

String concatenation, for example: str1 = "ABCD"; str2 = "EFG"; str1+str2.

Do not store the temporary pool (in the heap):

Using str.Tostring, for example: str1 = "ABCD"; str2 = str1.ToString ()

Use char [] .Tostring (), for example: str1=ABCD "; char [] charArray = str1.ToArray (); str2 = charArray.ToString ()

Use new String (), for example:

Str1= "9999"; char [] charArray = str1.ToArray (); string str2= new string (charArray); string str3 = new string (charArray); char [] charArray = {'Aguilar Magnum B'}; str1=" ABCDE "; str2=" CDE "+ charArray.Tostring (); char [] charArray1 = {' Achndrich Magnum B'}; char charArray2 = {'Corel"; str1= "ABCDE"; str2=charArray1.ToString () + charArray2.ToString () This is the end of the article on "what is the principle of String type storage in c #". Thank you for reading! I believe that everyone has a certain understanding of "what is the storage principle of String type in c #". If you want to learn more knowledge, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report