C # sharp experience the twelfth talk string

xiaoxiao2021-03-06  76

Twelfth talk string

String ??????? String is an instance of the System.String (or simplify the lowercase String) class in C #, which represents a constant character sequence. The syntax of the string is very simple: string s = "c # sharp XP"; that is, the content of the content as a string "C # Sharp XP" on the hosted stack, and S is just a reference handle to the memory area. . Look at the following code line: String S1 = "Hello, World!"; String S2 = S1; ??????? At this time, S1 and S2 point to the same block containing "Hello, World!" Memory area. C # provides two escape expressions for strings. The first type of escape character in traditional C / C is the same, that is, the expression of a counter-slope "/" plus atrical character. If "/ t" indicates the Tab key, "/ R" is represented by the carriage return, "/ n" means a wrap, etc. We have to indicate a file path under the C disk "My Document", string mypath = "c: // Documents and settings // cornfield // my documents // myfile.cs", where double slash is essential Diagonal line. The second representation method, we can add a "@" symbol before the string, you can express strings according to the normal character sequence. If the file path above can be expressed in this way: string mypath = @ "C: / Documents and settings / cornfield / my documents / myfile.cs ", the same as the previous expression. The second representation is useful in the case where we express the long strings containing special characters, it can even express special characters such as enter-in-laws, wraps such as characters. You can get a single character in the string (16-bit Unicode encoding) in the string - this and the string array in C / C is essentially different. The following code demonstrates this: string s = "c # sharp xp"; for (int index = 0; index

The following code example, a focus on the behavior of the above method: use system; class test {???? public static void main () ???? {??????? String S1 = "Hello, World! "; ???????? String S2 = s1.clone (). TOSTRING (); // cloned, handle equal, content equal ??????? String S3 = String.copy (S1); // Copy, the handle is not, the content is equal ?????????????????????????????????????????????????????????????? WriteLine (S1 == S3); // true, content equal ???????????????? console.writeline (Object) S1 == (Object) s2); // true, Handle reference is equal ???????? console.writeLine ((Object) S1 == (Object) S3); // false, handle reference does not wait ????}}}} The constant string constant string It means that once the value of the string is created, it cannot be changed. We look at a typical code line: string s1 = "hello,"; string s2 = "world!"; S1 = S2; ??????? The above statement is completed, S1 = "Hello, World!", S2 = "World!". However, the original value of S1 "Hello" did not disappear, exactly "Hello," still occupied memory space, but now I can't be quoted by us, it can only wait for .NET's automatic garbage collector to recover. Resources, this has caused memory leak in previous C . That is to say, we will have "Hello, World!" (The point to the point to the S1 reference handle), "World!" (S2 reference) The value indicated by the handle), "Hello," (without a reference handle pointing this value) a total of three memory cells that store strings. ??????? The character string value is also manifested as a String type parameter transmission On, let's see the following example: use system; class test {???? public static void main () ???? {???????? String P = "Hello,"; ????? ???? mymethod (p); ???????? console.writeline (P); // output hello, ????} ???? public static void mymethod (string P) ???? {???????? p = "world!"; ???????? console.writeline (P); // output hello, world! ????}} ?????? While we say that the String type is a reference type in C #, because of the constant nature of the string, our method MyMethod does not change the reference handle of the passed P, if you want to change, you need to use REF keyword modified parameters And this reference handle itself points to the expression string "Hello," memory area has not changed, and naturally, the string P in the MYMETHOD method will not change. Understand the characteristics of the string value of the string value is very important to us in the C #, such as the System.String class. Many ways to actually do not change the string of the participating operation, but create a new value, just put the original The string throws the auto garbage collector.

转载请注明原文地址:https://www.9cbs.com/read-119995.html

New Post(0)