Search⌘ K
AI Features

Fundamentals of C#

Explore the fundamentals of C# programming by understanding key data types such as int, double, bool, char, and string. Learn how to declare, initialize, and use variables correctly while avoiding common errors. Gain practical knowledge of arithmetic, comparison, logical, and compound assignment operators, along with using Math functions like power and square root for calculations.

Data types in C#

Every variable in C# needs to be declared with the help of a data type. It indicates whether a variable can store only numbers or can also store other symbols as its value.

Common data types

The following are the commonly used data types in C#:

  • int: Integer values (5, 0, -1, etc.)
  • double: Floating-point values (2.33333, -2500.001, 20.0, etc.)
  • bool: Boolean values (true, false).
  • char: Character values (a, 0, $ etc)
  • string: String values (educative, C#, etc.)

Variable declaration

The C# language requires the programmer to indicate the data type of the variables used in a program. The following code demonstrates the declaration of variables to input their values with suitable prompts and to display them with suitable labels:

class Test
{
    static void Main()
    {
        string str; // Declaring a variable of type string
        int number; // Declaring a variable of type integer
        double real; // Declaring a variable of type double
        bool isTrue; // Declaring a variable of type bool
        char letter; // Declaring a variable of type char

        System.Console.WriteLine("Please input a word: ");
        str = System.Console.ReadLine();
        System.Console.WriteLine("Please input an integer: ");
        number = int.Parse(System.Console.ReadLine());
        System.Console.WriteLine("Please input a real number: ");        
        real = double.Parse(System.Console.ReadLine());
        System.Console.WriteLine("Please input a boolean value true or false: ");
        isTrue = bool.Parse(System.Console.ReadLine());
        System.Console.WriteLine("Please input any character: ");
        letter = char.Parse(System.Console.ReadLine());

        System.Console.WriteLine("The value of each variable is enclosed in brackets: ");
        System.Console.WriteLine("str (" + str + ")");
        System.Console.WriteLine("number (" + number + ")");
        System.Console.WriteLine("fraction (" + real + ")");
        System.Console.WriteLine("isTrue (" + isTrue + ")");
        System.Console.WriteLine("letter (" + letter + ")");
    }
}
Variable declaration

Explanation

  • Lines 5–9: In these lines, the variables are declared so that they can be used to store values input by the user. There are different types of variables that are declared;
    • str is a variable of the string type.
    • number is a variable of the int type.
    • real is a variable of the double type.
    • isTrue is a variable of the bool type.
    • letter is a variable of the char type.
  • Lines 11–20: These lines output a prompt using System.Console.WriteLine(), demand input from the user using System.Console.ReadLine(), and then store them in the relevant variable.
  • Lines 22–27: These lines output the variable name as a label and the value enclosed in a bracket.

The following code demonstrates the declaration of the variables without inputting their values from the user:

C#
class Test
{
static void Main()
{
string str; // Declaring a variable of type string
int number; // Declaring a variable of type integer
double real; // Declaring a variable of type double
bool isTrue; // Declaring a variable of type bool
System.Console.WriteLine("The value of each variable is enclosed in brackets: ");
System.Console.WriteLine("str (" + str + ")");
System.Console.WriteLine("number (" + number + ")");
System.Console.WriteLine("fraction (" + real + ")");
System.Console.WriteLine("isTrue (" + isTrue + ")");
}
}

In the above code, lines 5–8 declare the variables str, number, real, and isTrue, with the string, int, double, and ...