C # language series lecture (13) - string

xiaoxiao2021-03-06  74

String strings are a instance of System.String (or simplified to lowercase String) classes in C #, which represents a constant character sequence. The syntax created by the string is very simple: string s = "c # sharp XP", that is, the contents of the content as the string "C # Sharp XP" on the hosted stack, and S is only a reference handle to the memory area. Look at the following code: 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. 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 to single oblique line. The second representation method can be added to a "@" symbol before the string, so that the string can be expressed in the normal character sequence, as the above file path can be expressed like this: 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 a long string containing special characters, it can even express special characters such as enter-in-law, wrap, etc. 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, the above-described method of the above method: use system; class test {spread s1 = "hello, world!"; String s2 = s1.clone (). TOSTRING (); / / Clon, handle, etc. = S3); // true, content equal console.writeline ((object) S1 == (Object) S2); // true, handle reference equal console.writeLine ((Object) S1 == (Object) S3); / / False, handle reference}} The constant meaning of the string constant string means that once the value of the string is created, it cannot be changed. Look at the typical code below: String S1 = "Hello,"; string s2 = "world!"; S1 = S2; After the above statement is executed, S1 is "Hello, World!", S2 is "World!". However, the original value "Hello" did not disappear, and "Hello" still has memory space, but now I can't be quoted by us, it can only wait for .NET's automatic garbage collector to recycle its resources. This causes memory leak in the previous C , which means that we will have "Hello, World!" (S1 reference handle points to the value), "World!" (S2 reference handle pointing Value), "Hello," (without a reference handle pointing this value) a total of three memory areas of the string. The character string value is also manifested on the parameter transmission as a string type, we look at 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!}} Although String type In C # is a reference type, but due to the constant nature of the string, the method MyMethod does not change the reference handle of the passed P (if you want to change, you need to use the REF keyword modification parameters), and this reference handle itself points to The memory area of ​​the expression string "Hello" has not changed, and naturally, the calling string P through the MyMethod method will not change. Understanding the characteristics of the string value is very important to us in the C #, such as the System.String class, many ways do not change the string itself involved in the operation, but create a new value, just put the original The string is left to the auto garbage collector. If the string is frequent in the C # program, it is possible to cause a large number of "creation / discarding" actions, which must cause the system to make the system, and we should use the StringBuilder class.

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

New Post(0)