Search⌘ K
AI Features

Introduction to Strings

Explore how strings work in C# by understanding their immutability, creation methods, and how to safely check for empty or null values using built-in .NET functions. Gain knowledge of advanced string literals like verbatim and raw strings, and how to access characters within strings. This lesson gives you a solid foundation for working effectively with text in your .NET applications.

The string type is an alias for the System.String class. It is an immutable data type that represents a sequence of Unicode characters:

string someText = "Hello World!";

Here, we declare a string variable and assign it a standard text literal. We must account for string immutability when designing our applications. Immutable means that we cannot change the string. When we want to modify a string object, we just create a new one.

Instead of changing a property of an existing string object, the runtime creates a new object with the modified value. Remember that creating a new object involves allocating memory for that object.

Note: Constantly allocating memory for new class instances may significantly degrade application performance.

Be aware of this fact before generating strings. ... ...