A variable in C# is a named storage location in the computer’s memory that holds data. Variables are used to store values that your program can manipulate, such as numbers, text, or other types of data. In C#, variables must be declared with a specific data type, which defines the kind of data the variable can hold.


Syntax for Declaring Variables

dataType variableName = initialValue;
  • dataType: Specifies the type of data the variable will hold (e.g., int, string, bool).
  • variableName: A unique name you assign to the variable.
  • initialValue (optional): An initial value can be assigned during declaration.

Example:

int age = 25;         // Declares an integer variable with a value of 25
string name = "John"; // Declares a string variable with a value of "John"
bool isActive = true; // Declares a boolean variable with a value of true

Types of Variables in C#

1. Local Variables

  • Declared inside a method and can only be used within that method.
  • Must be initialized before use.

Example:

void DisplayMessage()
{
    string message = "Hello, C#!";
    Console.WriteLine(message); // Output: Hello, C#!
}

2. Instance Variables

  • Declared inside a class but outside any method.
  • These are also called fields and belong to the instance of the class.

Example:

class Person
{
    public string name; // Instance variable
    public int age;     // Instance variable
}

3. Static Variables

  • Declared using the static keyword inside a class.
  • Shared among all instances of the class.

Example:

class Calculator
{
    public static int counter = 0; // Static variable
}

Rules for Naming Variables

  • Variable names are case-sensitive (e.g., Age and age are different).
  • Must start with a letter or an underscore _.
  • Can contain letters, numbers, and underscores.
  • Cannot use reserved keywords as variable names (e.g., int, class).

Common Data Types for Variables

Data TypeDescriptionExample
intInteger (whole numbers)int age = 30;
floatFloating-point numberfloat pi = 3.14f;
doubleDouble-precision numberdouble e = 2.718;
decimalHigh-precision decimal numberdecimal price = 19.99m;
charSingle characterchar grade = 'A';
stringSequence of characters (text)string name = "Alice";
boolBoolean (true/false)bool isReady = true;

Example: Declaring and Using Variables

using System;

class Program
{
    static void Main()
    {
        // Variable declarations
        int age = 25;
        string name = "John";
        bool isStudent = true;

        // Using variables
        Console.WriteLine($"Name: {name}");
        Console.WriteLine($"Age: {age}");
        Console.WriteLine($"Is Student: {isStudent}");
    }
}

Output:

Name: John
Age: 25
Is Student: True

Default Values for Variables

In C#, uninitialized variables of instance fields get default values:

  • Numeric types (int, double) default to 0.
  • bool defaults to false.
  • string and reference types default to null.

Example:

class Example
{
    int number; // Default is 0
    string text; // Default is null
    bool flag; // Default is false
}

Variable Scope

  • Local Scope: Variables declared inside a method are accessible only within that method.
  • Class Scope: Instance and static variables are accessible throughout the class.

Example:

class ScopeExample
{
    int instanceVariable = 10; // Class scope

    void MethodExample()
    {
        int localVariable = 20; // Local scope
        Console.WriteLine(instanceVariable); // Accessible here
        Console.WriteLine(localVariable);    // Accessible here
    }
}

Constant Variables

If a variable’s value should never change, use the const keyword.

Example:

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

Conclusion

Variables in C# are fundamental building blocks that allow you to store and manipulate data. By understanding how to declare, use, and manage variables, you’ll have a strong foundation for writing efficient and readable C# programs.

A variable in C# is a named storage location in the computer’s memory that holds data. Variables are used to store values that your program can manipulate, such as numbers, text, or other types of data. In C#, variables must be declared with a specific data type, which defines the kind of data the variable can hold.


Syntax for Declaring Variables

dataType variableName = initialValue;
  • dataType: Specifies the type of data the variable will hold (e.g., int, string, bool).
  • variableName: A unique name you assign to the variable.
  • initialValue (optional): An initial value can be assigned during declaration.

Example:

int age = 25;         // Declares an integer variable with a value of 25
string name = "John"; // Declares a string variable with a value of "John"
bool isActive = true; // Declares a boolean variable with a value of true

Types of Variables in C#

1. Local Variables

  • Declared inside a method and can only be used within that method.
  • Must be initialized before use.

Example:

void DisplayMessage()
{
    string message = "Hello, C#!";
    Console.WriteLine(message); // Output: Hello, C#!
}

2. Instance Variables

  • Declared inside a class but outside any method.
  • These are also called fields and belong to the instance of the class.

Example:

class Person
{
    public string name; // Instance variable
    public int age;     // Instance variable
}

3. Static Variables

  • Declared using the static keyword inside a class.
  • Shared among all instances of the class.

Example:

class Calculator
{
    public static int counter = 0; // Static variable
}

Rules for Naming Variables

  • Variable names are case-sensitive (e.g., Age and age are different).
  • Must start with a letter or an underscore _.
  • Can contain letters, numbers, and underscores.
  • Cannot use reserved keywords as variable names (e.g., int, class).

Common Data Types for Variables

Data TypeDescriptionExample
intInteger (whole numbers)int age = 30;
floatFloating-point numberfloat pi = 3.14f;
doubleDouble-precision numberdouble e = 2.718;
decimalHigh-precision decimal numberdecimal price = 19.99m;
charSingle characterchar grade = 'A';
stringSequence of characters (text)string name = "Alice";
boolBoolean (true/false)bool isReady = true;

Example: Declaring and Using Variables

using System;

class Program
{
    static void Main()
    {
        // Variable declarations
        int age = 25;
        string name = "John";
        bool isStudent = true;

        // Using variables
        Console.WriteLine($"Name: {name}");
        Console.WriteLine($"Age: {age}");
        Console.WriteLine($"Is Student: {isStudent}");
    }
}

Output:

Name: John
Age: 25
Is Student: True

Default Values for Variables

In C#, uninitialized variables of instance fields get default values:

  • Numeric types (int, double) default to 0.
  • bool defaults to false.
  • string and reference types default to null.

Example:

class Example
{
    int number; // Default is 0
    string text; // Default is null
    bool flag; // Default is false
}

Variable Scope

  • Local Scope: Variables declared inside a method are accessible only within that method.
  • Class Scope: Instance and static variables are accessible throughout the class.

Example:

class ScopeExample
{
    int instanceVariable = 10; // Class scope

    void MethodExample()
    {
        int localVariable = 20; // Local scope
        Console.WriteLine(instanceVariable); // Accessible here
        Console.WriteLine(localVariable);    // Accessible here
    }
}

Constant Variables

If a variable’s value should never change, use the const keyword.

Example:

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

Conclusion

Variables in C# are fundamental building blocks that allow you to store and manipulate data. By understanding how to declare, use, and manage variables, you’ll have a strong foundation for writing efficient and readable C# programs.