Strings Overview


In some languages, strings are null-terminated arrays of characters and there is no separate class for strings. In C#, strings are represented by the class string. Thus, strings are reference types.

String literals are actually string objects. For example, in the statement

Console.WriteLine("Hello world");

the literal "Hello world" causes the construction of a string object.

Strings are Immutable

Strings are immutable, which is to say, that they cannot be changed after they have been created. To implement changes, new strings can be created from existing strings and unused strings are automatically garbage collected.

A string is a reference object, so a string reference may be changed to refer to another string. However, the contents of any individual string cannot be changed.

.Net has another class called StringBuilder which is found in the System.Text namespace. Unlike standard strings, strings of the type StringBuilder may be changed.