# Understanding the String Class

A C# string is a read-only sequence of characters; read them by index, write special characters with escape sequences, and understand why strings never change.

Novice · C# · Updated November 3, 2026

## After this lesson you can

- Read a string's characters by index and find its Length
- Write special characters in a string with escape sequences

## Key takeaways

- A string is characters in order: `team[0]` is the first `char`, and `Length` says how many there are, so the last is at `Length - 1`.
- Strings never change. Methods such as `ToUpper` return a new string, so store it if you want to keep it: `name = name.ToUpper();`.
- Write special characters with escape sequences: `\n` for a new line, `\t` for a tab, `\"` for a double quote and `\\` for a backslash.
- An `@` in front of a string makes it verbatim, so the backslashes in `@"C:\bootcamp\strings"` are just backslashes.
- The empty string `""` has a `Length` of 0; `string.IsNullOrEmpty` checks for it and for `null`, which means no string at all.

Text is everywhere in programs: names, addresses, messages, web pages, file contents. In C#, text is a `string`, and you've used strings since your first program. This chapter looks inside them.

## A string is a sequence of characters

A string is an ordered sequence of `char` values, one per character. Like an array, each character has an index starting at 0, and `Length` says how many there are:

```csharp
string team = "Georgia";

Console.WriteLine(team.Length);
Console.WriteLine(team[0]);
Console.WriteLine(team[team.Length - 1]);
```

```text
7
G
a
```

`team[0]` is a `char`, the single character `'G'`. You can loop through a string's characters just as you loop through an array:

```csharp
string word = "loop";

foreach (char letter in word)
{
    Console.Write(letter + "-");
}
Console.WriteLine();
```

```text
l-o-o-p-
```

## Strings never change

Strings are **immutable**: once a string exists, its characters can't be changed. You can't assign to `team[0]`. Every operation that seems to change a string actually makes a **new** string:

```csharp
string name = "Vanessa";
string fullName = name + " Jones";
string shouting = name.ToUpper();

Console.WriteLine(name);
Console.WriteLine(fullName);
Console.WriteLine(shouting);
```

```text
Vanessa
Vanessa Jones
VANESSA
```

`name` is still `"Vanessa"`. To keep a changed version, store the new string, even in the same variable: `name = name.ToUpper();`.

## Special characters: escape sequences

Some characters can't be typed straight into a string, such as a double quote (it would end the string) or a new line. You write them as an **escape sequence**: a backslash followed by a letter or symbol.

| Escape | Means |
|---|---|
| `\n` | a new line |
| `\t` | a tab |
| `\"` | a double quote |
| `\\` | a backslash |

```csharp
Console.WriteLine("Line one\nLine two");
Console.WriteLine("She said \"hello\".");
Console.WriteLine("Name:\tGeorgia");
Console.WriteLine("C:\\bootcamp\\strings");
```

```text
Line one
Line two
She said "hello".
Name:	Georgia
C:\bootcamp\strings
```

## Strings with backslashes: verbatim strings

Windows file paths are full of backslashes. Put `@` in front of a string to make it **verbatim**: backslashes are just backslashes, and nothing is escaped.

```csharp
string folder = @"C:\bootcamp\strings";
Console.WriteLine(folder);
```

```text
C:\bootcamp\strings
```

## Empty strings

`""` is the **empty string**: a string with no characters, and a `Length` of 0. `string.IsNullOrEmpty` checks for it, and also for `null`, which means no string at all:

```csharp
string middleName = "";

Console.WriteLine(middleName.Length);
Console.WriteLine(string.IsNullOrEmpty(middleName));
```

```text
0
True
```

## Check yourself

2 questions to try on the lesson page.

## So what

A string is characters in order, numbered from 0, and it never changes in place. Every string method you'll meet next follows from those two facts.

## Key terms

- [string](https://www.ask-codey.com/glossary/string-c-sharp)
- [char](https://www.ask-codey.com/glossary/char-c-sharp)
- [String Index](https://www.ask-codey.com/glossary/string-index-c-sharp)
- [String Immutability](https://www.ask-codey.com/glossary/string-immutability-c-sharp)
- [Escape Sequence](https://www.ask-codey.com/glossary/escape-sequence-c-sharp)
- [Verbatim String](https://www.ask-codey.com/glossary/verbatim-string)

## Cheat sheets

- [Understanding the String Class](https://www.ask-codey.com/cheat-sheets/strings-in-csharp-understanding-the-string-class-cheat-sheet)

Source: https://www.ask-codey.com/lessons/strings-in-csharp-understanding-the-string-class
