System.String Class

The string in C# is handled by the System.String class. Before you can create and use strings, you must import the System namespace. There are multiple ways to create a string.

String str1;
str1 = "An example of a string.";

Notice that a string is enclosed in double quotation marks (). This indicates to the compiler that the data you are storing to the variable is a string.

The System.String class is mapped to the string keyword thanks to CTS. This allows you to use the keyword string, instead of the class System.String when creating a string.

string myString; 
myString = "An example of a string.";

You can declare a string and assign it a value in one statement, like this:

string myString = "This is another string";

You can also use the new keyword and the System.String constructor to assign value to the string.

String myString = new String("This is a string.");
string myString = new string("This is a string");

In .NET, a string is a reference type but behaves very much like a value type. Consider the following example of a typical reference type:

Button btn1 = new Button();
btn1.Text = "Button 1 ";
Button btn2 = btn1; // Assign address of btn1 to btn2

btn2.Text += " and Button 2";
Console.WriteLine(btn1.Text);
Console.WriteLine(btn2.Text);
Button 1 and Button 2
Button 1 and Button 2

We created a Button named btn1 and assign the Text property with a string. Next, we assigned the reference of btn1 to another Button named btn2. They are now referring to the same button. We now modify the Text property of btn2 by concatenating (combining) another string. Note that btn2.Text contains the Text from btn1 because they are referring to the same button. After the modification, we output the contains of the Text for each button. The result is the same. The moment that we modified the Text of btn2, the Text of btn1 was modified as well. (It is evident in the output of the WriteLine() statements).

If strings are indeed reference types, then the same behavior can be expected with the following code:

string str1 = "String 1";
string str2 = str1;

Now, str1 and str2 should now be pointing to the same instance. If we modify the value of str2, then str1 should be modified as well.

str2 += " and String 2";

Let’s print the values of the strings.

Console.WriteLine(str1);
Console.WriteLine(str2);
String 1
String 1 and String 2.

You can see that the values are different. The value of str1 was copied to str2. They do not point on the same instance. Therefore, changes you make with str2 will not reflect str1. A string can’t be a value type because it can handle a value an unpredictable length of characters.

A string is basically a collection of Unicode characters. The following code shows this by splitting each character using a foreach loop.

string str1 = "This is a string";
foreach (char c in str1)
{
   Console.WriteLine(c);
}
T
h
i
s

i
s

a

s
t
r
i
n
g

In fact, another version of the System.String’s constructor accepts a character array.

char[] charArray = { 'H', 'e', 'l', 'l', 'o' };

String myString = new String(charArray);

The next lesson shows you how to compare strings and demonstrates a simple example of sorting strings.