C # string details

Source: Internet
Author: User

C # string details
The most commonly used method is string, but sometimes some problems are easy to make mistakes without careful consideration. Today I will summarize the usage of string. 1. string is a reference type. We usually compare the string object, comparing the object value rather than the object itself, such as: string strA = "abcde"; string strB = "abc "; string strC = "de"; Console. writeLine (strA = (strB + strC); // trueConsole. writeLine (object) strA = (object) (strB + strC); // false because the string content is the same but the referenced instance is not the same 2. the string object is an unchangeable string strA = "abcde"; strA = "aaaaa"; on the surface, it seems that the contents of strA have been modified. In fact, "abcde" is not modified, instead, a new object "aaaaa" is created, and the reference of this object is assigned to strA. Finally, "abcde" is recycled as garbage. 3. create a direct value for string: string strA = "abcde"; // create a string object whose content is abcde, and then assign the reference of this object to the strA structure: char [] arr = {'A', 'B', 'C', 'D', 'E'}; string strA = new string (arr ); // only one type is listed here. Note: No String str = new String ("abcde"); string is.. NET Framework. 3. the string parameter is passed as the reference type. We try to change this value in a function and copy the code static void Main (string [] args) {string strA = "abcde "; deal (strA); Console. writeLine (strA); Console. readLine ();} static void Deal (string str) {str = str. substring (0, 2);} copying code result: abcde reason: When a parameter of the reference type is passed through a value, it is possible to change the data pointed to by the reference, such as the value of a certain type of member. However, the value of the reference itself cannot be changed. This problem can be solved by passing parameters through the ref keyword. Copy the code static void Main (string [] args) {string strA = "abcde"; Deal (strA); Console. writeLine (ref strA); Console. readLine ();} static void Deal (ref string str) {str = str. substring (0, 2);} copy the code result: AB transmits the reference itself, not the copy 4. null String and null String: no memory is allocated. The null String is allocated with memory, but no data is in the memory. copy the code static void Main (string [] args) {string strA = "1"; string strB = string. empty; string strC = null; Console. writeLine (int. parse (strA )); // Correct Console. writeLine (int. parse (strB); // The format of the input string is incorrect Console. writeLine (strC. toString (); // the instance where the object is not referenced. Console. readLine ();} indicates whether the built-in method string of the copied code is null or null. IsNullOrEmpty is equivalent to if (str = null | str. equals (String. empty) IsNullOrWhiteSpace is equivalent to if (str = null | str. equals (String. empty) | str. trim (). equals (String. empty) 5. stringBuilder copies the code string strA = "abc" for (int I = 0; I <10000; I ++) {strA + = "abc" ;}consolse. writeLine (strA); copy the code although the code will appear to append new characters to an existing string named strA using String concatenation, it actually creates a new String object for each concatenation operation. This greatly reduces performance. You can use the StringBuilder class to change the String value multiple times instead of the String class. The StringBuilder object is variable. When you append or delete a substring in a String, no new object is created, instead, modify the original object. After modifying the value of the StringBuilder object, you can call its StringBuilder. the ToString method converts it to the string StringBuilder strA = new StringBuilder (); for (int I = 0; I <10000; I ++) {strA. append ("abc");} Consolse. writeLine (strA. toString ());

Related Article

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.