C-SHARP - 3.6 String

This video is a short introduction to strings in C#; a dedicated section will explore them in depth later. A string is a sequence of characters surrounded by double quotes. Just like any other variable, you declare the type, give a name and assign a value: string prenom = "Mehdi";. We can build a third string from two existing variables by concatenating with the + operator: string nom = prenom + " " + family;. Concatenation works but can quickly become hard to read for many fragments.

To produce cleaner output, the static method string.Format uses a format pattern with indexed placeholders such as {0} and {1} followed by the variables in order. Another static method, string.Join, lets you combine all the elements of an array into a single string separated by a chosen character, for instance string.Join(",", new int[] { 1, 2, 3 }) gives "1,2,3". You can also access an individual character with an index: char c = prenom[0]; returns the first letter.

Immutability and special characters

  • Strings are immutable: you cannot change a character in place — any modification produces a new copy.
  • \n inserts a new line, \t a tab; these are escape characters.
  • To write a literal backslash you need \\, which becomes unreadable on file paths.
  • Prefix the string with @ to obtain a verbatim string and write the backslashes only once.
string name = prenom + " " + family;
string formatted = string.Format("{0} {1}", prenom, family);
string joined = string.Join(",", new int[] { 1, 2, 3 });
char firstLetter = prenom[0];

string path = @"C:\Users\Computer\Desktop\Project";

The Microsoft documentation lists every special character; the ones above are those you will use most often. Next we move to Visual Studio to practise these notions live.

Summary

This lesson introduces C# strings as sequences of characters enclosed in double quotes. It covers string declaration, concatenation using the + operator and String.Format() method, accessing individual characters via indexing, and demonstrates String.Join() for combining array elements. The lesson emphasizes string immutability in C# and explains escape sequences like \n and \t for special characters.

Key points

  • Strings are sequences of characters declared with the `string` keyword and enclosed in double quotes
  • Use String.Format() with placeholders ({0}, {1}) for cleaner, more readable string construction compared to + concatenation
  • Access individual characters using index notation (e.g., firstName[0] retrieves the first character)
  • Strings are immutable in C# — string methods return new strings rather than modifying the original
  • Escape sequences like \n (newline), \t (tab), and \\ (literal backslash) handle special characters in string literals
  • String.Join() combines array elements with a specified separator for efficient string building from collections

FAQ

What is the difference between string concatenation with + and String.Format()?

The + operator directly joins strings together, which becomes difficult to visualize with multiple variables. String.Format() uses a format string with placeholders ({0}, {1}, etc.), making the output structure clearer and easier to maintain.

Why are strings immutable in C#, and how can I modify them?

Immutability ensures string integrity and thread safety. To work with modified string content, use string methods like String.Format() or String.Join() which return new strings; you cannot change individual characters directly in an existing string.

When should I use String.Join() instead of concatenation?

String.Join() is ideal when combining array or collection elements with a consistent separator. It is cleaner, more efficient, and more readable than manually concatenating multiple values, especially with longer lists.