.NET Tutorial - Variables

.NET Tutorial - Variables

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