DEV Community

Cover image for Beginner C#: Getting Started
Sam Walpole
Sam Walpole

Posted on • Updated on • Originally published at samwalpole.com

Beginner C#: Getting Started

Now that I've convinced you why you should learn C#, now it's time to start learning. In this article, you will learn how to install Visual Studio and write your first C# program.

What is Visual Studio?

Visual Studio is an integrated development environment (IDE), meaning that it contains all of the tools you need to write, build, run and debug your code. It is primarily build for .NET applications, so it is the recommended IDE for C# developers. It has lots of other cool features too, which you kind find on the official website. Furthermore, the Community Edition of Visual Studio is completely free, and that's what we'll be using in this series.

Installing Visual Studio

First, you need to get the Visual Studio installer. Head over to the Visual Studio download page and download the Community Edition. At time of writing, the most recent version is Visual Studio 2019.

Once that has downloaded, run the installer and follow the instructions on the screen. At some point you will come to a screen that gives you the option to select certain workloads.

Visual Studio Workloads

Selecting a workload will install all of the relevant tools and software development kits (SDKs) necessary for that particular workload. For now, I would recommend just selecting the 'ASP.NET and web development' workload, although you may wish to select more depending on your interests.

You may also wish to click on the Language packs tab. If your native language is not English, this will allow you to select your own language that will be used inside Visual Studio.

Visual Studio Language Packs

Once you're ready, click install and wait for it to complete. You're now ready to start developing in Visual Studio!

Your First Program: Hello World

When you open Visual Studio, you will be prompted with a number of options. First select 'Create a new project'. N.B. I have a dark theme installed on my version of Visual Studio so yours will be a different colour.

Create a new project

You will then be prompted to choose a project template. For this example, you will want to choose Console App (.NET Core).

Select project template

You will then be asked to choose a name for the project and a location. Once you've done that, click Create.

Project name and location

You should now see a screen that looks something like this:

Project screen

On the right you will see that we have a Solution called HelloWorld, containing a project called HelloWorld, containing a single C# file (Program.cs).

If you're wondering what a Solution is by the way, it's basically a way to group multiple projects together. At the moment, we only have one project, but as an application grows more complex, it is common to split it in to multiple projects to make it easier to deal with. All of these projects will come under the same solution.

At the heart of every C# program, there is a Program class with a Main method. This is the entry point of the application and the code that will be run when the application starts. You will see that the template has already added a line of code to the Main method. Let's run the application to see what it does. To do this, click on the 'Play' button at the top of the screen, or go to Debug -> Start Debugging.

Hello World console

You should see Hello World! printed to the console. This is because the Console.WriteLine method takes a string a prints it to the console. A string is a data type in C# that allows you to store text data. In C#, strings are surrounded by quotation marks ("). Try replacing "Hello World!" with a different string.

Now that we've made and run our first program, let's try to make it a bit more interactive. We're going to modify the code so that it asks for our name and will then greet us by our name. Here is the new code:

using System;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("What is your name?");

            string name = Console.ReadLine();

            Console.WriteLine("Hello " + name + "!");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

The first line prints the sentence "What is your name?" to the console. In the second line, Console.ReadLine waits for the user to type something in to the console and press enter. Whatever they wrote gets saved to the variable called name, which is a string.

In C#, an every other programming language, variables are a way to store data in your program to be used at a later time. We can the recall the value of the variable by using it's name in code. Variables can store different types of data, but in this case we we are storing the string that comes from whatever the user typed in. You will learn more about variables later in this series.

The third line looks a little more complex. Here we are doing something known as string concatenation. Basically it means that we are adding each of these strings together to make one longer string. Try running the program and you will see what I mean.

Hello name

Here I have typed my name, Sam, into the console. This means that the name variable now contains the string "Sam". That means the string concatenation is actually doing "Hello " + "Sam" + "!", which results in the string "Hello Sam!" being printed to the console.

Conclusion

Congratulations! You have now installed the Visual Studio IDE and written your first program in C#. You have learnt the two main console commands that you will come across, and have had a small introduction into variables and data types. Later in this series we will discuss those again in greater detail.

To make sure that you don't miss any posts in this series, please follow this blog and subscribe to my newsletter. If you found this post helpful, please like it and share it. You can also find me on Twitter. If you want, you can also buy me a coffee ! 😊


This post is part of a beginner C# series. I will update this section with links to each post when each new post is published.

  1. Why learn C#?
  2. Getting Started
  3. Variables and Basic Data Types
  4. Working with Strings

Top comments (0)