Some knowledge points about strings in C #

Source: Internet
Author: User

CodeFor example, refer to the original article.

 

1. A string is a string object whose value is text. Internally, these texts are stored as a read-only set of char objects, where each object represents a Unicode Character encoded in a UTF-16. C # The string does not end with null (this is different from C and C ++ ), therefore, the C # string can contain any number of embedded null characters ("\ 0 "). String Length indicates the number of characters, whether or not these characters are formed by a unicode proxy. To access each Unicode code bit in a string, use the stringinfo object.

 

2. in C #, the string keyword is the alias of system. String. Therefore,StringIt is equivalent to string. You can select a naming convention based on your preferences.StringClass provides many methods for securely creating, operating, and comparing strings. In addition, the C # language also loads some operators to simplify common string operations. For more information about keywords, see string (C # reference ). For more information about types and methods, see string.

 

3. Note: Do not use the new operator to create string objects except when initializing strings using character arrays.

 

4. Use the empty constant value to initialize a string. You can create a String object with zero length. The string representation of a zero-length string is "". Using the empty value instead of null to initialize a string can reduce the possibility of nullreferenceexception. Use the static isnullorempty (string) method to verify the string value before trying to access the string.

 

5.The string object is unchangeable.: They cannot be changed after they are created.All seemingly modified stringsStringBoth the method and the C # operator actually return results in the form of a New String object.In the following example, when the content of S1 and S2 is connected to form a string, the two original strings are not modified. The + = Operator creates a new string containing the combined content. This new object is assigned to the variable S1, and the object originally assigned to S1 is released because no other variable contains references to it for garbage collection.

 

6. The format string is a string that the content can be dynamically determined at runtime. Create a format string using the static format method and embed placeholders in braces. These Placeholders are replaced with other values at runtime.

An overload of the writeline method uses format strings as parameters. Therefore, you can embed only the format string without explicitly calling this method. However, if you use the writeline method to display debugging output in the Visual Studio "output" window, you must explicitly call the format method, because writeline only accepts strings, but does not accept format strings. For more information about format strings, see format types.

 

7. An empty string is an instance of the system...:. String object that does not contain characters. In various programming schemes, empty strings are often used to represent blank text fields. Empty strings can be called because they are valid system .. ::. string object. on the contrary, the Null String does not reference system .. ::. string object. Any attempt to call a method for a null string will generate nullreferenceexception. However, the null string can be used with other strings in the concatenation and comparison operations.

 

8. The string operations in. NET are highly optimized and will not significantly affect performance in most cases. However, in some application scenarios, for example, in a compact loop of hundreds or even thousands of times, string operations may affect the performance. The stringbuilder class creates a string bufferProgramIt provides better performance when performing a large number of string operations. The stringbuilder string also enables you to reassign individual characters (characters not supported by the built-in string data type ).

 

9. Because the string type implements ienumerable <(of <(T>), you can use the extension method defined in the enumerable class for the string. For the string type, to avoid visual confusion, these methods are excluded from intelliisense, but these methods are still available. In addition, you can use a LINQ query expression for a string. For more information, see LINQ and strings.

 

Original article address:

Http://msdn.microsoft.com/zh-cn/library/ms228362.aspx

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.