Introduction

This tutorial 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.

Tip

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

Run your first c# program

Run the following code in the interactive window. Select the Enter focus mode button. Then, type the following code block in the interactive window and select Run:

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.

Declare and use variable

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"; Console.WriteLine(aFriend);

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"; Console.WriteLine(aFriend);

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);

Select Run again to see the results. You've been using + to build strings from variables and constant strings. There's a better way. You can place a variable between { and } characters to tell C# to replace that text with the value of the variable. This is called String interpolation. If you add a $ before the opening quote of the string, you can then include variables, like aFriend, inside the string between curly braces. Give it a try:

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

Select Run again to see the results. Instead of "Hello {aFriend}", the message should be "Hello Maira".

Work with strings

Your last edit was our first look at what you can do with strings. Let's explore more. You're not limited to a single variable between the curly braces. Try this:

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

As you explore more with strings, you'll find that strings are more than a collection of letters. 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.");

Tip 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. The window has a feature called IntelliSense that makes suggestions for what you can do. Type a . after the d in firstFriend. You'll see a list of suggestions for properties and methods you can use.

Congratulations

You've completed the "Hello C#" introduction to C# tutorial. You can select the Numbers in C# link below to start the next interactive tutorial, or you can visit the .NET site to download the .NET Core SDK, create a project on your machine, and keep coding. The "Next steps" section brings you back to these tutorials. For further reading on the string type:

  • C# Programming Guide topic on strings.
  • How to tips on working with strings.