Introduction To C# And Visual Studio For Beginners

C# Tutorial For Beginners

In this tutorial we’ll start looking at the C# programming language. C# is a popular programming language that can be used to create programs for tablets, phones, web servers, and desktops. The Syntax of C# is similar to Java, C++, and JavaScript and is one of many languages that can be used for .NET programming. If you’re ready to learn about programming in C#, Microsoft makes available a wonderful community edition of their powerful Visual Studio IDE. We’ll make use of Visual Studio Community during these next few C# tutorials.

Download And Install Visual Studio Community

By using the link above, you’ll be able to install Visual Studio Community. Once installed, the initial start page looks like this.
microsoft visual studio community start page

The .NET Framework

With Visual Studio now installed, the. NET Framework is also installed since it is part of the Visual Studio installation. Writing applications in C# with Visual Studio leverages the. NET Framework. This means applications can take advantage of all kinds of services that the framework provides. This includes things like saving information to a database, reading information from an XML file, as well as configuration, network operations, and all the basic core features that nearly every application needs. C# can therefore be used to build business applications, games, web apps, and applications that run on tablets or mobile devices. The .NET Framework does it’s work by way of the Common Language Runtime and the Framework Class Library.

The CLR manages your application

  • Memory management
  • Operating system and hardware independence
  • Language independence

CLR and FCS for dot net

Framework class library

  • A library of functionality to build applications

microsoft framework class library

Working With C#

C# is a popular language and nice to work with if you are familiar with C, C++, Java, and JavaScript. Let’s use Visual Studio to create our first C# program. C# is a compiled language, so in order to actually see the results of the code you write, you will need to compile it first. Let’s see how to create a super simple console application in Visual Studio now. You can select File->New->Project->Visual C#-> Console App(.NET Framework).
Microsoft Visual Studio Console Application

Visual Studio will set up some boilerplate code for us which looks like this.

Solution Explorer In Visual Studio

Once the new project is created, we also see the solution explorer. The Solution Explorer window organizes all of the projects code and as well as other projects. In the screenshot below, we see that WelcomeToCSharp is highlighted which represents the current project. Inside of that project we have three folders. Those are bin, obj, and Properties. There is also an App.config file, a Program.cs file, and a Visual C# Project File named WelcomeToCSharp.csproj.
Visual Studio Solution Explorer

Namespaces In C#

Notice at the top of the Program.cs file that there are several lines of code we might not be familiar with.

What are these lines all about? These are namespaces that Visual Studio included by default as they are commonly used namespaces. For example, if we were to use something from the Tasks namespace, you can see the line using System.Threading.Tasks; is now black, while the others are light gray indicating they are not being used.
csharp used and non-used namespaces

For now we can delete those namespaces so that our Program.cs looks like this.

Now, we are making use of our own namespace, which is WelcomeToCSharp. So the class of Program is really something like WelcomeToCSharp.Program. Let’s write something out to the console then!

Building And Running Your C# Application

Let’s put this program in to action. To see the result of our code so far, we can click on Debug, and then Start Without Debugging.

Pretty Sweet! We got our first C# console application to run. Visual Studio automatically compiled the source code and launched the program in a new console window. Visual Studio automatically added the “Press any key to continue . . .” dialog to make the console window go away. If this wasn’t provided, you’d see a command window flash on the screen for a moment and that’s about it. The program can also be run from the command line by typing the name of the executable, WelcomeToCSharp.exe.

Welcome To C#!

Passing A Parameter To Our Program

In order to customize our first C# Program a little more, let’s add support for passing in a Parameter. Here is how we can do that.

With the updated source code, we’ll need to build the program in order to update the executable file for the project. This can be done by clicking on Build->Build Solution or with the keyboard shortcut of Ctrl+Shift=B.
visual studio build solution

Now, we can run the program again from the command line and pass in a string parameter like so. Note that when specifying the name of the program, you can omit the .exe from the end of the file when running it.

C:UsersusersourcereposWelcomeToCSharpWelcomeToCSharpbinDebug>WelcomeToCSharp Tom
Welcome To C#, Tom!

Just for grins, we can run it again and pass in a different string parameter just to see it in action.

C:UsersusersourcereposWelcomeToCSharpWelcomeToCSharpbinDebug>WelcomeToCSharp Friend
Welcome To C#, Friend!

Preventing Runtime Errors

We’ve got our program accepting a parameter so that it displays a custom message. What happens if we don’t pass that argument when running the program? Well, let’s see.


Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at WelcomeToCSharp.Program.Main(String[] args) in C:UsersusersourcereposWelcomeToCSharpWelcomeToCSharpProgram.cs:line 9

windows checking for solution to problem

Ouch. We get a nasty error message, and the program crashes. We can modify the program so that if an argument is not provided at runtime, we won’t get that Index was outside the bounds of the array error message. Here is the updated code.

Now, when we run the program it will not crash if we don’t provide an argument. Below we run the program once with no argument, then again with an argument. The program now has logic in the form of a simple if / else statement so that it can behave differently depending on what was passed to it.

Welcome To C#, Friend!

C:UsersusersourcereposWelcomeToCSharpWelcomeToCSharpbinDebug>WelcomeToCSharp Partner
Welcome To C#, Partner!

Using Console.ReadLine

Another approach we could take to the program is to prompt the user for information while the program is running. We can do this with the Console.ReadLine method.

Now when the program runs, we are prompted to enter some data. The program does not move forward until we do so.

Now, we can enter a name when prompted and the program responds accordingly.

Let’s take this idea a little further. Now, we want to ask the user if they had their breakfast today. Based on if they did or not, we’ll know if they’re ready to learn some good stuff about C# and Visual Studio. Our modified code now will look like this.

Running the program with two users Tom, and Jared, we can see that one has had his breakfast and is ready to learn. The other is working on an empty stomach and needs to fuel up before moving forward.


Hi there, what's your name?
Did you eat breakfast today? Y or N
Welcome To C#, Tom!
Glad to see you're ready to learn!


Hi there, what's your name?
Did you eat breakfast today? Y or N
Welcome To C#, Jared!
You better get some food!

Introduction To C# And Visual Studio For Beginners Summary

In this tutorial we had a look at the community edition of Visual Studio and put it to work by building a really simple program. It wasn’t anything incredible, but it gives us an idea of how C# works with Visual Studio. You could also easily make use of other IDE programs, but Visual Studio is hard to beat for a Microsoft language. Even this simple program introduced us to some important concepts such as types, classes, variables, and simple expressions. It was just enough to become familiar with the Visual Studio workflow. Some key points to remember.

  • C# is a strongly typed and case-sensitive language for .NET
  • Every object in C# has a specific type
  • You can write a class to define a type
  • Thousands of types are built into the .NET framework
  • Code to be executed must live inside a type
  • You can define custom types
  • Visual Studio is an IDE to work with C# applications of all types

Next up, we’ll look at Classes and Objects in C#!