C-SHARP - 3.7 String (Demo)
We are back on Visual Studio to practise C# strings. To create a string, we declare a variable of type string, give it a name, and assign a value between double quotes: string mot = "Bonjour";. We add a second variable string prenom = "Medji";. To build the sentence Bonjour Medji we concatenate with the + operator and add an explicit space between the words, otherwise the two values would stick together. We display the result with Console.WriteLine(phrase);.
Concatenation works, but as we saw in the previous video, string.Format makes the output far easier to visualise. The method takes a format pattern with placeholders {0}, {1}, and so on, followed by the variables in order. We can also insert literal text between the placeholders to obtain sentences like Bonjour, je m'appelle Medji with a single call.
string mot = "Bonjour";
string prenom = "Medji";
string phrase = mot + " " + prenom;
Console.WriteLine(phrase);
Console.WriteLine(string.Format("{0}, je m'appelle {1}", mot, prenom));
string path = @"C:\Users\Computer\Desktop\Project";
Console.WriteLine(path);
Special characters
\ngoes to a new line in the middle of a string.\tinserts a tab.\\writes a literal backslash, useful for file paths.- Prefix the string with
@to obtain a verbatim literal and avoid every doubled backslash.
Without the @ prefix, a file path like "C:\Users\Computer\Desktop\Project" is misinterpreted because the compiler reads each \ as an escape sequence. Doubling every backslash works but is unreadable. Prefixing the string with @ tells the compiler to take the content literally, which makes the code much clearer. That is all for this video, see you in the next lesson.
Summary
This practical C# lesson demonstrates string operations in Visual Studio, covering variable creation, concatenation using the + operator, and formatted string output with String.Format(). The demo also illustrates the use of special escape characters like newlines (\n) and tabs (\t) for controlling string display.
Key points
- String variables are declared with the `string` type and assigned text in quotation marks
- The + operator concatenates multiple strings; explicit spaces must be included to separate words
- String.Format() creates formatted strings using numbered placeholders {0}, {1}, etc., for dynamic variable insertion
- Escape sequences like \n (newline), \t (tab), and \\ (backslash) control how strings are displayed
- Console.WriteLine() outputs strings to the console for visual verification
FAQ
How do you combine two strings in C#?
Use the + operator to concatenate them, e.g., `string result = "Hello " + name;`. Remember to include spaces explicitly if needed.
What is String.Format() and how does it work?
String.Format() builds formatted strings using a template with numbered placeholders {0}, {1}, etc., followed by the corresponding variables. For example, `String.Format("Hello {0}, I am {1}", name, age)`.
How do you create a line break in a string?
Insert the escape sequence \n into your string. For example, `"Hello\nWorld"` displays as two separate lines when printed.