Common. Net misunderstandings 1-1 strings cannot be modified (immutable)

Source: Internet
Author: User

There is always a lot of information orArticleThe. NET string cannot be modified (immutable)

 

 

Let's take a look at what a string is.

In. net, the string is a reference type, but it has many value-type features.

For example, to compare two strings, we actually compare the two contents, instead of the reference address (Here we refer to the concept of string resident technology)

 

Then let's see why some people often say that the string cannot be modified.

Because strings have the following features:

    1. When you perform operations on strings such as + and substring, a new string is returned,
    2. Generally, you cannot find a method to modify the string. (assign a value directly to the method but modify the address. The original value is not changed)
    3. The string in the main C # shows many value types.

 

How to modify strings

Refer to the followingCode:

Define a pointer to a string and modify the value based on the address.

 

    Unsafe  
{
String S1 = " 1 test string " ;
Fixed ( Char * P = S1)
{
* P = ' 2 ' ;
}
Console. writeline (S1 );
}

 

About string resident

A string generally has only one instance in the memory, that is, a string can be used in multiple places at the same time.

If the content of a string is modified, other strings will be affected at the same time. refer to the following code: when S1 is modified, S2 is also modified.

 

 

    Unsafe 
{
String S1 = " 1 test string " ;
String S2 = " 1 test string " ;
Fixed ( Char * P = S1)
{
* P = ' 2 ' ;
}
Console. writeline (S1 );
Console. writeline (S2 );
}

 

 

 

 

PS1: String resident is an interesting technique. We will discuss this in detail in later sections.
PS2: directly modifying strings is a very dangerous behavior. 1. It will affect the entire application.ProgramPlace where this string is used. 2. It will overwrite other places outside the string accidentally (the representation of the string in memory is described later)

PS3: Distribution of strings in memory

The. NET string consists of three parts in the memory.

1. The Int value of 4B indicates the length of the string (before the low position)

A) when comparing strings, the internal optimization result is to compare the length first. If the length is different, false is returned immediately; otherwise, the content will be checked again.

B) This is an int value. It implies that the maximum length of a string on a 32-bit system is the maximum length of an int, which is about 2 GB.

C) if the length of the string you want to modify changes the original length, such as "a"-> "A1", please remember to modify this value .... otherwise, you will find that the modified string is broken.

2. Contents of the string itself (Unicode-16 encoding)

3. 4B string terminator (\ 0)

 

 

Not to be continued (I just started to use the blog park... I found that C # category is gray and cannot be selected... I don't know what's going on)

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.