Lesson Weekend

Most of the information we worked with in Introduction to Programming, such as strings, integers, variables, methods, and objects, function similarly in C#. There are a few differences but you'll catch on quickly! In this lesson we'll learn about one of the most common data types: the string.


C# strings are very similar to JavaScript strings. They're simply a sequence of characters between two quotation marks. Note that strings require quotation marks specifically -- apostrophes (') are used to define a different type of variable, chars.

We can open our REPL with the $ dotnet script command in the Terminal (for Mac) or PowerShell (for Windows) to try them out.

Once in the REPL we'll type a basic string and hit Enter:

> "Hello World"

Here is the output:

"Hello World"

Now let's try it with a semi-colon.

> "Hello World";

Adding a semi-colon produces an error.

(1,1): error CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement

Remember that C# is a strongly typed language. According to this error, we've already broken one of C#'s strict rules about typing. In this case, the semi-colon indicates that C# is expecting a command. In other words, you need to do something with this string. Let's take the error's advice and make an assignment, which means we'll assign our string to a variable:

> string greeting = "Hello";

We're no longer using JavaScript's let or const. Instead, we're being more specific and declaring that greeting is a string. Let's call the variable in the REPL:

> greeting

Without the semi-colon, our REPL can still return the value of the variable. A quick word of warning: don't get used to solving problems in C# by dropping semi-colons. This convention helps us evaluate code in the REPL, nothing more.


We can also concatenate strings together with the + operator. As you'll recall, concatenation is the act of making a new string out of multiple smaller strings:

> "Hello" + " " + "World"
"Hello World"

Notice the return value is different from what we originally typed. This is the evaluation part of the REPL (Read, Evaluate, Print, Loop) acronym; C# has evaluated the code and returned a result.

Here's another example:

> "Programming" + " " + "is" + " " "awesome!"

(1,34): error CS1002: ; expected

Oops, we got an error this time! The error says a semi-colon was expected. Something is wrong with our syntax and the REPL doesn't understand the command.

This error points us to a missing + between the space after "is" and the string "awesome". Let's fix this:

> "Programming" + " " + "is" + " " + "awesome!"
"Programming is awesome!"

There we go! Play around with strings a bit more in the REPL before advancing to the next lesson.


  • String: A sequence of characters between two quotation marks which is generally used for text content.

  • Concatenate: The act of making a new string out of multiple smaller strings, usually with a + operator, like this: "Hello" + " " + "World", which creates the string "Hello World".


Here's an example of concatenating multiple strings together in the C# REPL:

> "Programming" + " " + "is" + " " + "awesome!"
"Programming is awesome!"


  • In C#, all lines of code must end with a semi-colon (;). There are a few exceptions we'll learn later, but don't worry about this for now.

  • We can open the REPL with the $ dotnet-script command in the Terminal (for Mac) or GitBash (for Windows).

Lesson 2 of 5
Last updated more than 3 months ago.