C#: Variables

This entry is part 7 of 71 in the series C#

Variables are just like the variables that we learnt in Mathematics when we were in the ninth grade or secondary school.

Every variable has a type that determines what values can be stored in the variable.

Below, we assign a value of 25 to a variable age that is of type integer (int).

int age = 25

Similar to the original C programming language, C# variable declaration is case-sensitive.

The declaration below will create 2 variables age and Age.

int age = 25
int Age = 25

C# is a type-safe language, and the C# compiler guarantees that values stored in variables are always of the appropriate type.

Variable must be definitely assigned

A variable must be definitely assigned before its value can be obtained.

It has to be declared and assign a specific value.

Commonly used variables
using System;

namespace Variables
    class Program
        static void Main()
            int point = 90;
            string greeting = "Hello, World!";
            double bigNumber = 1e100;

            Console.WriteLine("{0} {1} {2}", point, greeting, bigNumber);


The above example shows the commonly used variable types integer, string and double.

In line 13, notice how we need to put the variables into array like {} in the writeline line to output the 3 variables.

The {0} denotes the first variable and {1} denotes the second variable and so on.

Series Navigation<< C#: ASCII and Unicode
C#: Declaring implicit variables using var >>

Leave a comment

Leave a Reply