What Does String Mean?
String, in the context of .NET, is a class representing a read-only text containing Unicode characters, which can be used to manipulate its contents. The String class is used for related operations such as concatenation, search, comparison, sorting, formatting, copying and displaying text. Strings also aid the development of globalized and localized applications by providing options for applying culture-sensitive (specific or current culture) conventions for string operations wherever applicable. For example, strings used internally have to be handled in a common way, while user-specific data such as file names, XML tags, etc. need to be culture-sensitive.
Techopedia Explains String
String in the .NET Framework considers Null as a character in the string, which results in the string operations (such as comparison, length, copy, etc.) executed in the .NET environment, but not in the same way as execution in native C/++ code. Selection of the appropriate string manipulation method, which is done from the set of overloads of this class to suit the requirement of application, is vital while using this class.
The contents of text stored in a String object are immutable, which implies that its value cannot be changed after its creation. In case of string manipulation functions such as string concatenation, a new string object is created and used to pass it as a return value. Unlike the String class, StringBuilder class is mutable and used in string manipulation operations without penalty on performance.
The following are best practices while using String class:
- The right overload specifying the string comparison rule (method overload with StringComparison parameter) has to be used.
- For culture-agnostic string matching and performance reasons, usage of method, StringComparison.Ordinal or StringComparison.OrdinalIgnoreCase is better.
- For checking equality of strings, overload of String.Equals method can be used. For sorting purposes, Compare and CompareTo can be used.
- Overload methods need to be used without passing default values.
- Rather than references, the ‘==’ operator can be used to compare two String objects for checking the equality of their contents.