.NET Tutorial - Variables

Subscribe to my newsletter and never miss my upcoming articles

C# is Microsoft's popular programming language used to make websites, mobile apps, video games, VR, and more!

Intro

Purpose

Install .NET and create your first application.

Prerequisites

None.

Scenario

A simple application written in C# to create variables of different data types, assign values, and output the results to the Console window.

Let's get started

Download and install

To start building .NET apps you just need to download and install the .NET SDK (Software Development Kit).

Head over to dotnet download and install for your platform.

Check if the installation was successful?

On your terminal, PowerShell or command line type dotnet If the command runs, printing out information about how to use dotnet, you're good to go.

Running C# on Visual Studio Code

NOTE: It is highly recommended that you actually type in the code and not simply copy and paste. Typing in the code allows you to evaluate the syntax as you enter it and helps to reinforce your learning along the way.

Create your App

In your terminal, run the following commands:

~$ dotnet new console -o myApp
~$ code myApp

The dotnet command creates a new application of type console for you. The -o parameter creates a directory named myApp where your app is stored and populates it with the required files. The code myApp command opens myApp in Visual Studio Code.

Peek 2019-09-22 15-48.gif

The main file in the myApp folder is Program.cs. It already contains the necessary code to print "Hello World!" to the Console.

On your VsCode Terminal or your preferred Terminal enter:

~$ dotnet build && dotnet run

The dotnet build complies the code and check for errors while dotnet run executes the source code which prints Hello World! to the Console.

Peek 2019-09-22 16-01.gif

Congratulations, you've built and run your first .NET app!

Now let's learn something new.

Edit your code

Change the assign values with your details if you wish

Open Program.cs and remove Console.WriteLine("Hello World!"); and enter the following code to create variables of different data types, assign values and output the results to the Console window:

using System;

namespace myapp
{
    class Program
    {
        static void Main(string[] args)
        {
            // TODO: create variables of different data types
            // TODO: initialize with a "default" value

            string firstName;
            string lastName;
            int age;
            string street;
            string city;
            string country;
            DateTime birthDate;

            // TODO: assign some values
            firstName = "Vincent";
            lastName = "Iroleh";
            age = 24;
            street = "7 Chief Ubani Street Eziama";
            city = "Aba";
            country = "Nigeria";
            birthDate = new DateTime(1995, 4, 19);

            // TODO: output to the console window
            // TODO: use simple output with just a variable name

            Console.WriteLine(firstName);
            Console.WriteLine(lastName);

            // TODO: use placeholder style
            Console.WriteLine("{0} years old.", age);

            // TODO: use string concatenation 
            Console.WriteLine(street + ", " + city + ", " + country);

            // TODO: use string interpolation
            Console.WriteLine($"Born on {birthDate}");

        }
    }
}
  • Press the CTRL + F5 keys to start the application without debugging.
  • This will cause Visual Studio Code to compile the code and then run the application. A console window opens displaying the result id no errors are encountered.

  • Use can also enter dotnet build && dotnet run to build and run your app from the terminal.

Peek 2019-09-22 16-57.gif

If you run into issues, you can compare your code with mine HERE

Summary

  • We created variables of data types of string, int, and DateTime which we assigned later.
  • We printed to the Console firstName and lastName.
  • We used a placeholder style to output the age.
  • We did string concatenation by printing street, city and country.
  • Finally, we used string interpolation to output the date of birth.

Keep Learning

David Brown's photo

hey! thanks for a nice article and resource for learning! Do you know other good online resources to learn C#? If you have some on your mind please share, thanks in advance! I'm currently doing an online course at codeasy.net - a nice one, still need more practice...

Vincent Iroleh's photo

Hi David Brown you can checkout Scott Allen's C# Fundamentals course on Pluralsight.

pluralsight.com/courses/csharp-fundamentals..

I am personally learning with him through his courses on pluralsight. Its a great course to check out.

Thanks.

David Brown's photo

Thanks a lot for the advice! Vincent Iroleh

Kristen Parker's photo

I use it in scientific research on neural networks. I am testing the capabilities of the human brain and the possibilities of remembering. We can transfer over here at EduBirdie to code and load it onto external matrices. A person with an external carrier is a reality. We can design information that is in an external flash drive. I think education can be easy.

Mark Shepherd's photo

Thanks a lot for your advice. It's very useful. I'm pretty sure that there is a people who can write my dissertation literature review for me. Please, give me help.

Harry Stuart's photo

Does this data remain up-to-date? For dissertation literature review services, updated info is a rule.

Margo Adams's photo

If you are concerned about your reputation, we assure you that getting help in writing a dissertation conclusion from the experienced writer working at PrimeWritings.co.uk will help you get rid of stress and frustration.