Characteristics of C # strings

Source: Internet
Author: User

When it comes to strings, we naturally feel simple, but there are always some small problems looming, I will systematically say the question of string, there is no future to add.

1, first string is a class,string is just an alias of the String class, alias meaning: Another code name, is used as a string.

2,string str= "abc", and char[] cha={' A ', ' B ', ' C '} are different. Let's not simply know a string is a character array. To understand the essence.

is the invariant string, string is only readable, but not writable. But the character array is readable and writable.

Readable: Str[0]=a str[1]=b str[2]=c in strings, Str[0]=a str[1]=b str[2]=c in characters,

Writable: The elements in the string are not writable

Class program    {        static void Main (string[] args)        {            char[] cha = {' A ', ' B ', ' C '};            Console.WriteLine (Cha[0]);            Cha[0] = ' B ';            Console.WriteLine (cha);            String str = "abc";            Console.WriteLine (Str[0]);            Str[0] = ' d ';            Console.readkey ();        }

If str[0]= ' d '; "Error 1 cannot be assigned to a property or indexer" string.this[int] "-it is a read-only" character array.

It is suggested here that str= "abc"; str+= "D", Console.WriteLine (str); This is not an indication that the word nginx can be changed? Can you write it?

Let's take a look at this question and analyze it.

See this figure, compared to everyone's understanding of the character string invariance. One more diagram to prove this invariance.

3, this proves again that the string is immutable, and also raises the question of the concept of the "staging pool" of strings.

String str= "abc"; string str1= "abc" refers to the same object.

But this is just string str= "abc"; string str1= "abc, not char[] cha={' A ', ' B ', ' C '}; String Str=new string (CHA);

When debugging, open the Watch window, enter the * variable, monitor the address of the variable, both are the same address, proving the same variable pointed to. Different addresses, naturally different variables.

To summarize, the processing string must have accepted his return value, because each time it is processed, an object is produced.

The string instance is often ToCharArray (), and then the string's (char[]) constructor is called. The element in the modify string.

Question: This reminds us of the use of value passing and reference passing. Updated again later.

4,

String Str=null;

Str= "";

String str1=string. Empty;

String Str2= "";

String str3= "";//There is a space

After running the str=null, the address of STR has not changed and is still 0x00000000, so there is no allocated memory space.

When it finishes running, as shown, prove string. Empty and "" are the same, of course, and the first to assign null, and then "" the same. But having a space character is different.

Characteristics of C # strings

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.