A Comprehensive Resource for Microsoft Technologies

Welcome, your go-to destination for everything related to .NET and Microsoft technologies. With over 10 years of experience in the IT industry, I am excited to share my knowledge and insights on this platform. This blog is dedicated to providing valuable information, tips, and tutorials that are not only relevant to the rapidly evolving IT industry but also essential for individuals working on real-time projects and preparing for interviews

C# Tutorial 3.2: Variables and Data Types

 In C#, variables are used to store and manipulate data. Each variable has a specific data type that determines the kind of data it can hold. Let's explore how to declare variables and the fundamental data types in C#.

1. Declaring Variables:

In C#, you declare a variable by specifying its data type, followed by the variable name. Here's a basic example:

using System;

class Program
{
    static void Main()
    {
        // Variable declaration
        int age; // Declaring an integer variable named 'age'

        // Variable initialization
        age = 25; // Assigning the value 25 to the 'age' variable

        // Displaying the value of the variable
        Console.WriteLine("Age: " + age);
    }
}


In this example, an integer variable named age is declared, initialized with the value 25, and then displayed on the console.

2. Data Types:

C# supports various data types. Here are some of the fundamental ones:

  • Numeric Types:

    • int: 32-bit signed integer.
    • float: 32-bit single-precision floating-point.
    • double: 64-bit double-precision floating-point.
    • decimal: 128-bit high-precision decimal.
  • Boolean Type:

    • bool: Represents a Boolean value (true or false).
  • Character Type:

    • char: Represents a single Unicode character.
  • String Type:

    • string: Represents a sequence of characters.

3. Example Using Different Data Types:

Let's create a program that uses various data types:

using System;

class Program
{
    static void Main()
    {
        // Numeric types
        int age = 25;
        float height = 5.9f; // Note the 'f' suffix for float
        double weight = 160.75;
        decimal salary = 50000.50m; // Note the 'm' suffix for decimal

        // Boolean type
        bool isStudent = true;

        // Character type
        char grade = 'A';

        // String type
        string name = "John Doe";

        // Displaying values
        Console.WriteLine($"Age: {age}");
        Console.WriteLine($"Height: {height}");
        Console.WriteLine($"Weight: {weight}");
        Console.WriteLine($"Salary: {salary}");
        Console.WriteLine($"Is Student: {isStudent}");
        Console.WriteLine($"Grade: {grade}");
        Console.WriteLine($"Name: {name}");
    }
}

4. String Interpolation:

In the example above, string interpolation is used with the $ symbol before the string. This allows you to embed expressions inside string literals, making it more readable.

5. Run the Program:

Press F5 or click the "Start Debugging" button to build and run your program. You should see the output with values for each variable.

6. Experiment:

  • Try changing the values of variables and see how it affects the output.
  • Declare variables of different data types and experiment with their values.

Congratulations! You've learned about variables and fundamental data types in C#. In the next tutorial, we'll explore control flow statements, including if statements and loops.