Hello C#

This quickstart teaches you C# interactively, using your browser to write C# and see the results of compiling and running your code. It contains a series of lessons that begin with a "Hello World" program. These lessons teach you the fundamentals of the C# language.

You will learn how to...

Run your first C# program

Run the following code in the interactive window. To do that, type the following code block in the interactive window and click the Run button:

Console.WriteLine("Hello World!");

Congratulations! You've run your first C# program. It's a simple program that prints the message "Hello World!". It used the Console.WriteLine method to print that message. Console is a type that represents the console window. WriteLine is a method of the Console type that prints a line of text to that text console.

Let's move on and explore more. The rest of this lesson explores working with the string type, which represents text in C#. Like the Console type, the string type has methods. The string methods work with text.


This online coding experience is in preview mode. If you encounter problems, please report them on the dotnet/try repo.

Declare and use variables

Your first program printed the string "Hello World!" on the screen.


As you explore C# (or any programming language), you'll make mistakes when you write code. The compiler will find those errors and report them to you. When the output contains error messages, look closely at the example code, and the code in the interactive window to see what to fix. That exercise will help you learn the structure of C# code.

Your first program is limited to printing one message. You can write more useful programs by using variables. A variable is a symbol you can use to run the same code with different values. Let's try it! Replace the code you've written in the interactive window with the following code:

string aFriend = "Bill";

The first line declares a variable, aFriend and assigns it a value, "Bill". The second line prints out the name.

You can assign different values to any variable you declare. You can change the name to one of your friends. Add these two lines in the interactive window following the code you've already added:

aFriend = "Maira";

Notice that the same line of code prints two different messages, based on the value stored in the aFriend variable.

You may have also noticed that the word "Hello" was missing in the last two messages. Let's fix that now. Modify the lines that print the message to the following:

Console.WriteLine("Hello " + aFriend);

Click Run again to see the results.

As we progress, creating strings to print using + becomes cumbersome. String interpolation lets you piece together strings in a more concise and readable way.

If you add a $ before the opening quote of the string, you can then include expressions, like the aFriend variable, inside the string in curly brackets.

Console.WriteLine($"Hello {aFriend}");

Click Run again to see the results.


This online coding experience is in preview mode. If you encounter problems, please report them on the dotnet/try repo.

Work with strings

Your last edit was our first look at what you can do with strings. Let's explore more.

The + is called an operator in C#. When applied to strings like firstString + secondString, a new string is created with the text from the first followed by the text from the second. This is referred to as string concatenation. You can combine any number of strings, including string variables. Try this:

string firstFriend = "Maria";
string secondFriend = "Sage";
Console.WriteLine($"My friends are {firstFriend} and {secondFriend}");

As you explore more with strings, you can find the length of a string using Length. Length is a property of a string and it returns the number of characters in that string. Add the following code at the bottom of the interactive window:

Console.WriteLine($"The name {firstFriend} has {firstFriend.Length} letters.");
Console.WriteLine($"The name {secondFriend} has {secondFriend.Length} letters.");


This is a good time to explore on your own. You've learned that Console.WriteLine() writes text to the screen. You've learned how to declare variables and concatenate strings together. Experiment in the interactive window. Write some more messages yourself. Build strings using the + operator.


This online coding experience is in preview mode. If you encounter problems, please report them on the dotnet/try repo.

Do more with strings

You've been using a method, Console.WriteLine, to print messages. A method is a block of code that implements some action. It has a name, so you can access it.

The string type has many useful methods. Let's explore the Trim method, and related methods TrimStart and TrimEnd. These methods remove leading and trailing spaces:

string greeting = "      Hello World!       ";

string trimmedGreeting = greeting.TrimStart();

trimmedGreeting = greeting.TrimEnd();

trimmedGreeting = greeting.Trim();

This sample reinforces a couple of important concepts for working with strings. The methods that manipulate strings return new string objects rather than making modifications in place. You can see that each call to any of the Trim methods returns a new string, but does not change the original message.

There are other methods available to work with a string. For example, you've probably used a search and replace command in an editor or word processor. The Replace method does something similar in a string. It searches for a substring, and replaces it with different text. The Replace method takes two parameters. These are the strings between the parentheses. The first string is the text to search for. The second string is the text to replace it with. Try it for yourself. Add this code:

string sayHello = "Hello World!";
sayHello = sayHello.Replace("Hello", "Greetings");

Two other useful methods make a string ALL CAPS or all lower case. Try the following code:



This online coding experience is in preview mode. If you encounter problems, please report them on the dotnet/try repo.

Search strings

The other part of a search and replace operation is to find text in a string. You can use the Contains method for searching. It tells you if a string contains a substring inside it. Try the following code to explore Contains:

string songLyrics = "You say goodbye, and I say hello";

The Contains method returns a boolean value which tells you if the string you were searching for was found. A boolean stores either a true or a false value. You'll learn more about boolean values in a later lesson.


There are two similar methods, StartsWith and EndsWith that also search for sub-strings in a string. These find a substring at the beginning or the end of the string. Try to modify the previous sample to use StartsWith and EndsWith instead of Contains. Search for "You" or "goodbye" at the beginning of a string. Search for "hello" or "goodbye" at the end of a string.


Watch your punctuation when you test for the text at the end of the string. If the string ends with a period, you must check for a string that ends with a period.

You should get true for starting with "You" and ending with "hello" and false for starting with or ending with "goodbye".


This online coding experience is in preview mode. If you encounter problems, please report them on the dotnet/try repo.

Complete challenge

Did you come up with something like this?

string songLyrics = "You say goodbye, and I say hello";



This online coding experience is in preview mode. If you encounter problems, please report them on the dotnet/try repo.


You've completed the "Hello C#" quickstart.

You can learn more about working with the string type in the C# Programming Guide topic on strings. How to tips on strings.

You learned how to...

Next Tutorial

Numbers in C#


  • dotnet bot
  • pkulikov
  • Bill Wagner
  • Maira Wenzel
  • Ty Overby