Ebooks

C# basics

In this part of the C# tutorial, we will cover basic programming concepts of the C# language. We introduce the very basic programs. We work with variables, constants and basic data types. We read and write to the console; we mention variable interpolation.

C# simple example

We start with a very simple code example.

Program.cs
using System;

namespace Simple
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("This is C#");
        }
    }
}

This is our first C# program. It will print "This is C#" message to the console. We will explain it line by line.

using System;

The using keyword imports a specific namespace to our program. Namespaces are created to group and/or distinguish named entities from other ones. This prevents name conflicts. This line is a C# statement. Each statement is ended with a semicolon.

namespace Simple
{

We declare a namespace. Namespaces are used to organize code and to prevent name clashes.

class Program
{
    ...
}

Each C# program is structured. It consists of classes and its members. A class is a basic building block of a C# program. The above code is a class definition. The definition has a body, which starts with a left curly brace ({) and ends with a right curly brace (}).

static void Main(string[] args)
{
    ...
}

The Main() is a method. A method is a piece of code created to do a specific job. Instead of putting all code into one place, we divide it into pieces, called methods. This brings modularity to our application. Each method has a body, in which we place statements. The body of a method is enclosed by curly brackets.

The specific job for the Main() method is to start the application. It is the entry point to each console C# program. The method is declared to be static. This static method can be called without the need to create an instance of the Program class. First we need start the application and after that, we are able to create instances of classes. The Main() method has the args argument, which stores command line arguments.

Console.WriteLine("This is C#");

In this code line, we print the "This is C#" string to the console. To print a message to the console, we use the WriteLine() method of the Console class. The class represents the standard input, output, and error streams for console applications. Note that Console class is part of the System namespace. This line was the reason to import the namespace with the using System; statement. If we didn't use the statement, we would have to use the fully qualified name of the WriteLine() method: System.Console.WriteLine("This is C#");.

$ dotnet run
This is C#

Executing the program gives the above output.

C# console reading values

We can use the Console class to read values as well.

Program.cs
using System;

namespace ReadLine
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter your name: ");

            string name = Console.ReadLine();
            Console.WriteLine("Hello {0}", name);
        }
    }
}

The second program reads a value from a console and prints it.

string name = Console.ReadLine();

We read a line from the terminal. When we hit the Enter key, the input is assigned to the name variable. The input is stored into the name variable, which is declared to be of type string.

Console.WriteLine("Hello {0}", name);

In this code line, we do string formatting. The {0} specifier is replaced with the value of the name variable.

$ dotnet run
Enter your name: Jan
Hello Jan

This is the output of the second program.

C# command line arguments

C# programs can receive command line arguments. They follow the name of the program, when we run it.

Program.cs
using System;

namespace CommandLineArgs
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 0; i < args.Length; i++)
            {
                Console.WriteLine("{0}", args[i]);
            }
        }
    }
}

Command line arguments can be passed to the Main()method.

public static void Main(string[] args) 

The Main() method receives a string array of command line arguments.

for (int i=0; i<args.Length; i++) 
{
    Console.WriteLine("{0}", args[i]);
}

We go through the array of these arguments with a for loop and print them to the console. The Length property gives the number of elements in the array. Loops and arrays will be described in more detail later.

$ dotnet run 1 2 3
1
2
3

We provide three numbers as command line arguments and these are printed to the console.

C# variables

A variable is a place to store data. A variable has a name and a data type. A data type determines what values can be assigned to the variable, for instance integers, strings, or boolean values. Over the time of the program variables can obtain various values of the same data type. Variables are always initialized to the default value of their type before any reference to the variable can be made.

Program.cs
using System;

namespace Variables
{
    class Program
    {
        static void Main(string[] args)
        {
            string city = "New York";
            string name = "Paul"; int age = 35;
            string nationality = "American";

            Console.WriteLine(city);
            Console.WriteLine(name);
            Console.WriteLine(age);
            Console.WriteLine(nationality);

            city = "London";
            Console.WriteLine(city);
        }
    }
}

In the example we have four variables.

string city = "New York";

We declare a city variable of the string type and initialize it to the "New York" value.

string name = "Paul"; int age = 35;

We declare and initialize two more variables. We can put two statements on one line. But for readability reasons, each statement should be on a separate line.

Console.WriteLine(city);
Console.WriteLine(name);
Console.WriteLine(age);
Console.WriteLine(nationality);

We print the values of the variables to the terminal.

city = "London";

We assign a new value to the city variable.

$ dotnet run
New York
Paul
35
American
London

This is the output of the example.

The var keyword

Variables at a method scope can be implicitly typed using the var keyword. The variables are always strongly types, but with var the type is inferred by C# compiler from the right side of the assignment.

Program.cs
using System;

namespace ImplicitType
{
    class Program
    {
        static void Main(string[] args)
        {
            var name = "Peter";
            var age = 23;

            Console.WriteLine("{0} is {1} years old", name, age);

            name = "Jozef";
            age = 32;

            Console.WriteLine("{0} is {1} years old", name, age);

            Console.WriteLine(name.GetType());
            Console.WriteLine(age.GetType());            
        }
    }
}

In the program we have two implicitly typed variables.

var name = "Peter";
var age = 23;

On the left side of the assignment we use the var keyword. The name variable is of string type and the age of int. The types are inferred from the right side of the assignment.

Console.WriteLine(name.GetType());
Console.WriteLine(age.GetType());

We determine the types of the variables with GetType().

$ dotnet run
Peter is 23 years old
Jozef is 32 years old
System.String
System.Int32

This is the output.

C# constants

Unlike variables, constants retain their values. Once initialized, they cannot be modified. Constants are created with the const keyword.

Program.cs
using System;

namespace Constants
{
    class Program
    {
        static void Main(string[] args)
        {
            const int WIDTH = 100;
            const int HEIGHT = 150;
            int var = 40;

            var = 50;

            // WIDTH = 110;
        }
    }
}

In this example, we declare two constants and one variable.

const int WIDTH = 100;
const int HEIGHT= 150;

We use the const keyword to inform the compiler that we declare a constant. It is a convention to write constants in upper case letters.

int var = 40;

var = 50;

We declare and initialize a variable. Later, we assign a new value to the variable.

// WIDTH = 110;

This is not possible with a constant. If we uncomment this line, we get a compilation error.

C# string formatting

Building strings from variables is a very common task in programming. C# has the string.Format() method to format strings.

Program.cs
using System;

namespace StringFormat
{
    class Program
    {
        static void Main(string[] args)
        {
            int age = 34;
            string name = "William";

            string msg = string.Format("{0} is {1} years old.", name, age);
            Console.WriteLine(msg);
        }
    }
}

Strings are immutable in C#. We cannot modify an existing string. We must create a new string from existing strings and other types. In the code example, we create a new string. We also use values from two variables.

int age = 34;
string name = "William";

Here we define two variables.

string msg = string.Format("{0} is {1} years old.", name, age);

We use the Format() method of the built-in string class. The {0}, {1} are the places where the variables are evaluated. The numbers represent the position of the variable. The {0} evaluates to the first supplied variable and the {1} to the second.

$ dotnet run
William is 34 years old.

This is the output of the program.

C# string interpolation

Some dynamic languages like Perl, PHP or Ruby support string/variable interpolation. Variable interpolation is replacing variables with their values inside string literals. C# supports variable interpolation since C# 6.0.

Program.cs
using System;

namespace VariableInterpolation
{
    class Program
    {
        static void Main(string[] args)
        {
            string name = "Peter";
            int age = 34;

            string msg = $"{name} is {age} years old";

            Console.WriteLine(msg);
        }
    }
}

In the code example, we build a string using variable interpolation.

string msg = $"{name} is {age} years old";

The string is preceded with the $ character and the variables are inside curly brackets.

$ dotnet run
Peter is 34 years old

This is the output of the program.

This chapter covered some basics of the C# language.