C# String Data Type
The sixteenth part of the C# Fundamentals tutorial begins an examination of the string data type. This is possibly the most important native data type in the C# programming language. Using strings, we can perform processing of textual information.
C#'s string data type is used to define a series of Unicode characters. The series can be as short as zero characters in length, known as an empty string, or can be much longer. The theoretical maximum size is several billion characters. However, this can be unachievable, as the operating system may not provide enough memory to store such strings.
Strings Are Objects
Variable types in C# can be broadly categorised into two groups. These are value types and reference types. All of the data types that have been examined so far in this tutorial have been value types, or structures. The string data type is not a structure; it is a class. Classes differ from structures in ways that are beyond the scope of this tutorial. They are used in object-oriented programming, which will be described in a future tutorial. However, there are some key points about classes that you should know.
When a variable of a class type is created, or instantiated, the variable that is created is known as an object. So all strings are actually objects of type string. All objects can be set to null to indicate that they are undefined. Therefore any string object is nullable. Unlike the nullable numeric and character data types described earlier in this tutorial, this applies in all versions of the .NET framework.
A second important fact about objects is that rather than being value types, they are reference types. This means that the data in the object is stored somewhere in the computer's memory and the variable holds a pointer to that memory. This means that two objects of the same type can point to the same area of memory, in which case the two objects are actually the same thing; a change to one is mirrored in the other. This will become more important when we consider the creation of methods later in the tutorial.
An important property of strings is that they are read-only or immutable. Once a string has been created, its contents cannot be changed. This can be a surprise, even to experienced C# developers, as strings can apparently be created and modified easily. However, when a string is updated the .NET framework actually discards the original and creates a new string. For strings that are modified infrequently this allows more efficient processing. Frequently modified strings can quickly become inefficient; another class called StringBuilder should be used in these cases.
String Assignment Literals
The value of a string is assigned using the assignment operator (=). As with the character data type, when assigning a literal value delimiting symbols must be used to identify the start and the end information. Quotation marks (") are used for string literals.
string helloString = "Hello world";
As indicated earlier, strings are objects and therefore capable of holding null. Null can be assigned in a similar manner as when using nullable numeric data types.
string nullString = null;
A null string indicates that the variable is undefined. Often you will want to store an empty string. This differs from null as the value is defined, although it contains no characters. There are two ways to assign an empty string value. You can use a pair of quotation marks or the String.Empty value. The latter is sometimes preferred as some developers find it to be more readable.
emptyString = ""; // emptyString is empty
emptyString = String.Empty; // emptyString is still empty
All classes contain one or more constructors. A constructor is a special method that is executed when a new object of that class is instantiated. Constructors prepare the object's values and obtain any resources that it may require, sometimes using provided arguments you provide.
The string class defines several constructors. Most are beyond the scope of this tutorial. One interesting constructor allows you to specify a character and an integer to create a string containing a single character repeated many times. This constructor is used in the following example. Note the use of the new keyword that is used whenever objects are instantiated in this way, and the constructor parameters within parentheses.
string repeating = new string('.', 15); // repeating = "..............."
22 October 2006