Start learning free

LESSON 2 OF 413 · WELCOME TO C#

Introduction to Strings in C#

Lesson

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 fail to compile with the error Newline in constant



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() method 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