C# Variables & Keywords

In C#, a variable is a named storage location in memory that holds a value of a specific data type. Variables allow programs to store, modify, and retrieve data dynamically.

Declaring Variables

To declare a variable, specify the data type followed by the variable name:


int age;         // Declares an integer variable
string name;     // Declares a string variable
bool isActive;   // Declares a boolean variable
    

Initializing Variables

Variables can be assigned a value at the time of declaration or later:


int age = 25;             // Initialization during declaration
string name = "LoopCode";    // Initialization
bool isActive = true;     
    

Types of Variables

In C#, variables can be categorized based on their scope and lifetime:

  • Local Variables: Declared inside a method or block. Accessible only within that block.
    
    void Demo() {
        int localVar = 10;   // local variable
        Console.WriteLine(localVar);
    }
                
  • Instance Variables: Declared inside a class but outside methods. Each object has its own copy.
    
    class Person {
        public string name;  // instance variable
    }
    Person p = new Person();
    p.name = "Rahul";
                
  • Static Variables: Declared with the static keyword. Shared among all objects of the class.
    
    class Counter {
        public static int count = 0;  // static variable
    }
    Counter.count++;
                
  • Read-Only Variables: Declared with readonly. Value assigned once and cannot be changed.
    
    readonly int maxValue = 100;
                
  • Constants: Declared with const. Value cannot be changed after declaration.
    
    const double PI = 3.14159;
                

Rules for Variable Names

  • Must start with a letter or underscore (_)
  • Cannot start with a number
  • Cannot contain spaces or special characters (except _)
  • Cannot be a C# reserved keyword
  • Case-sensitive (age ≠ Age)

C# Keywords

Keywords are reserved words that have a special meaning in C#. They cannot be used as variable names.

  • int, float, double
  • if, else, for
  • while, do, switch
  • class, interface, namespace
  • true, false, null

Example: Variables & Keywords


// Valid variable names
int age = 25;
string firstName = "Rahul";
bool isActive = true;

// Using var keyword
var number = 100;      // Compiler infers type int
var text = "Hello";    // Compiler infers type string

Console.WriteLine(age);
Console.WriteLine(firstName);
Console.WriteLine(isActive);
Console.WriteLine(number);
Console.WriteLine(text);
    

Best Practices

  • Use meaningful variable names
  • Use camelCase for variable names
  • Do not use keywords as variable names
  • Initialize variables when possible

Conclusion

Variables and keywords form the foundation of C# programming. Understanding how to declare, initialize, and use them correctly is essential for writing readable and maintainable code.

C# Variables & Keywords Quiz

Q1. Which of these is a valid variable name?



Q2. Which variable is shared among all objects of a class?



Q3. What does var do in C#?


C# Introduction C# Variables & Keywords C# Data Types C# If Else Statement C# Loops C# Comments C# Type Casting C# User Input C# Operators C# Math C# String C# OOP Concepts C# Classes and Objects C# Multiple Classes and Objects C# Class Members C# Constructors C# Access Modifiers