What a string is
A string is a piece of text: any sequence of characters you can type, including letters, digits, spaces and symbols. Programs use strings for names, messages, and anything else people read. In C#, you write a string by putting the text between double quotes:
"Hello world"
That quoted text is called a string literal. C# gives such values the type string.
Printing strings
You can hand a string to Console.WriteLine to display it:
Console.WriteLine("My name is Andy");
Console.WriteLine("Welcome!");
My name is Andy
Welcome!
The quotes are not part of the string. They only tell C# where the text starts and where it ends, so they don't appear in the output.
Strings versus numbers
A value in quotes is text, even if it looks like a number. Console.WriteLine(3 + 4) prints 7, because C# adds two numbers. Console.WriteLine("3 + 4") prints 3 + 4, because the quotes make it text. Likewise, "1" + "1" joins two strings and gives 11, while 1 + 1 gives 2.
Joining strings and measuring them
The + operator puts strings together. A variable of type string can hold the text:
string name = "Andy";
Console.WriteLine("My name is " + name);
Console.WriteLine(name.Length);
My name is Andy
4
Length is the number of characters. Spaces count, and the empty string "" has length 0.
Putting a variable inside a string
Joining pieces with + gets awkward when there are many of them. C# also has interpolated strings: put a $ before the opening quote, and write any variable or expression in curly braces inside the text:
string name = "Andy";
Console.WriteLine($"Hello, {name}!");
Hello, Andy!
C# replaces {name} with the variable's value. Most C# programmers prefer this form once a message mixes text and values.
Characters are not strings
A string is a sequence of characters, and C# has a separate type for one character, char, written in single quotes. This difference has consequences. "A" + 1 gives the string A1, but 'A' + 1 is arithmetic on the character's numeric code and gives the number 66. When you want text, use double quotes, even for one letter.
Special characters
A double quote ends a string, so how do you put one inside? Start it with a backslash. A backslash followed by a character is an escape sequence:
| Write | Meaning |
|---|---|
\" | a double quote |
\\ | a backslash |
\n | a new line |
\t | a tab |
Console.WriteLine("She said \"hi\"");
Console.WriteLine("a\nb");
She said "hi"
a
b
Gotchas in C#
- Double quotes only. A string literal uses
"...". Single quotes make achar, a single character, as in'A'."A"and'A'are different types. - Don't forget the closing quote.
Console.WriteLine("Welcome!);doesn't compile, because C# reaches the end of the line still inside the string. The compiler reports an error such as "Newline in constant". Nothing runs until you fix it. Check that every opening quote has a partner. - Exact text. Case, spaces and punctuation are all part of the string, so
"Hello" == "hello"is false. - Strings can't be changed. They are immutable. A method like
ToUpper()returns a new string and leaves the original alone: afterstring a = "hi"; a.ToUpper();,ais still"hi". ==compares text. For strings, two values are equal when they contain the same characters, not only when they are the same object.
The exercise
The task is to print the text I love coding!, which takes a single statement:
- Call
Console.WriteLine. - Pass the text inside double quotes, with the words, spacing, and exclamation mark exactly as given.
- Finish with a semicolon.
Console.WriteLine("I love coding!");
The output is I love coding! without the quotes. If the test fails, compare your text character by character: a missing ! or a lowercase I is enough to fail it.
Key takeaways
- A string is text written between double quotes, and its type is
string. - The quotes mark the boundaries and are not part of the text.
- Use
+to join strings and.Lengthto count characters. - Write
\"for a quote inside a string, and\nfor a new line. - Every opening quote needs a closing one, or the program won't compile.