C# Variables

In programming, a variable similarly to a container for data. You can change the value of a variable while your program is running. A constant, on the other hand, is a value that does not change during any phase of your program execution.

Variables in C# store data that can be modified during program execution.

Rules for Naming Variables

  • The name must start with a letter or underscore (_).

  • It cannot include spaces or symbols (except for the underscore).

  • Avoid using reserved keywords in variable names.

How to Declare and Initialize Variables

  • To create a variable, first specify its data type, then a name, and optionally a value.

Example

int age = 30;           // Integer variable
string name = "John";   // String variable
bool isActive = true;   // Boolean variable

C# defines various types of variables based on their scope and purpose:

1. Local variables:

Local variables can only be used within the method or block where they are declared

​
void Example()
{
    int number = 5; // Local variable
    Console.WriteLine(number);
}

​

2. Instance Variables:

Instance Variables are declared within a class but not in any methods. These variables refer to a class instance.
​
class Car
{
    string color; // Instance variable
}

​

3. Static Variables:

Declared with the static keyword, so they are shared by all instances of the class.

class Counter
{
    static int count = 0; // Static variable
}

Constants in C#

Constants are similar to variables, but their values are fixed and cannot be changed once set. These are declared with the const keyword.

Example

const double Pi = 3.14159;
Console.WriteLine(Pi);

 

 Variables scope


The scope defines where a variable can be accessed. It's classified as:

  • Block Scope: Variables declared inside {} are only accessible within the braces.

  • Method Scope: Variables declared in a method are only available within that method.

  • Class Scope: Class-level variables are accessible throughout the class.

Example

​
class Program
{
    static int classVariable = 10; // Class scope

    static void Main()
    {
        int methodVariable = 20; // Method scope

        if (methodVariable > 10)
        {
            int blockVariable = 30; // Block scope
            Console.WriteLine(blockVariable);
        }

        // Console.WriteLine(blockVariable); // Error: Not accessible here
    }
}

​

Practice Program

Here’s a small example to understand variables and constants better:

​
class Program
{
    const double TaxRate = 0.15; // A constant for tax rate

    static void Main()
    {
        Console.Write("Enter price: ");
        double price = Convert.ToDouble(Console.ReadLine()); // Variable to store user input

        double tax = price * TaxRate; // Calculate tax
        double total = price + tax;   // Calculate total cost

        Console.WriteLine($"Tax: {tax}");
        Console.WriteLine($"Total: {total}");
    }
}

​


Key Points

  • Variables are used to store data that may change.

  • Constants contain fixed values.

  • Proper variable naming and scope leads to cleaner, more maintainable code.

Leave a comment

You must be logged in to post a comment.

0 Comments