Start learning free

LESSON 2 OF 347 · WELCOME TO C#

Introduction to Strings in C#: Text in Double Quotes

Lesson Read the full explanation

TL ; DR:

  • To create a string in C#, we put some text inside double quotes ("Hello world").


  • Be careful not to forget any quote! This code missing a " at the end:

    Console.WriteLine("Welcome!);

    would throw a SyntaxError



Full lesson:

How boring would our life as humans be if we communicated only using numbers? Luckily we have letters, words and languages to express what we think!

We can also use letters and words in C# to express more meaningful messages like we do in real life.


Strings:

In programming, a string is any block of text e.g. any sequence of characters from your keyboard (letters, numbers, spaces, symbols, etc.).

To create a string in C#, we put some text inside double quotes like this "Hello world".

For example, we can use strings inside the Console.WriteLine() function for printing messages to the console:

Console.WriteLine("My name is Andy");
Console.WriteLine("Welcome to AlgoCademy!");

The output of this code is:

My name is Andy
Welcome to AlgoCademy!


As you can see, the quotes are not printed. That's because quotes are not part of the string. Their job is solely to let C# know when a string declaration starts and when it ends.


Assignment
Follow the Coding Tutorial and let's work with strings!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 1
    Ctrl + Enter to check

    C# · WELCOME TO C#

    Introduction To Strings in C#, explained

    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:

    WriteMeaning
    \"a double quote
    \\a backslash
    \na new line
    \ta 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 a char, 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: after string a = "hi"; a.ToUpper();, a is 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:

    1. Call Console.WriteLine.
    2. Pass the text inside double quotes, with the words, spacing, and exclamation mark exactly as given.
    3. 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 .Length to count characters.
    • Write \" for a quote inside a string, and \n for a new line.
    • Every opening quote needs a closing one, or the program won't compile.
    Keep going with C#About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free