C# String Theory—What is a string?
Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio
| CSharp-Online.NET:Articles |
| C# Articles |
| edit |
What is a C# string?
The technical definition of "string" varies with the programming language. In C#, whereas a string is a sequential collection of Unicode characters that represents text, a String object is a sequential collection of System.Char objects that represents a string. A C# string has several properties which are critical to understanding how to use them.
Contents |
A string is a reference type.
A common misconception is that a C# string is a value type. This is understandable; because, in many situations it does act a bit like a value type. However, it is—in fact—a normal reference type to an object of type System.String.
A string is immutable.
Once created, the data value in a string object can never be changed: it is immutable (read-only) under normal circumstances. (The existence of the intern pool is the reason for immutability.) Methods that appear to modify a String object—in fact—return a new String object containing the modification. To modify the actual contents of a string-like object, the System.Text.StringBuilder class can be used.
The following example does not change the data in the string object; rather, it changes the value in the reference variable so that it points to a different immutable string.
string string1 = "has a relation"; string1 = string1.Replace ("has a", "is a");
This example creates four immutable strings—"has a relation", "has a", "is a", "is a relation". And, the string variable which formerly pointed to "has a relation", now points to "is a relation".
A string may contain nulls.
The String methods will handle null (\u0000) characters in string values; however, many classes—e.g. Windows Forms classes— may consider the string terminated at the first null. This behavior is familiar to C programmers who are accustomed to strings as character sequences ending with the nul (null) character. (Null here refers to the Unicode character \u0000 rather than to the C# keyword null.)
While the API does not consider strings to be null terminated, in the actual implementation, the character array which contains the string is null terminated. This allows the string to be passed directly to unmanaged (non-.NET) functions which accept Unicode without the necessity of copying or converting the string.
|

