C# Variables - Complete Guide
Introduction to Variables
Variables are containers for storing data values in C#. In C#, variables must be declared with a specific data type that determines the size and layout of the variable's memory, the range of values that can be stored, and the operations that can be applied.
C# is a strongly-typed language, which means you must declare the type of a variable before you can use it. This helps catch errors at compile time rather than at runtime.
Variable Declaration and Initialization
Variables can be declared and initialized in several ways:
Example
using System;
namespace VariableExamples
{
class Program
{
static void Main(string[] args)
{
// Declaration only
int age;
string name;
// Declaration with initialization
int score = 100;
string greeting = "Hello, World!";
double price = 19.99;
bool isActive = true;
char grade = 'A';
// Multiple declarations of the same type
int x = 5, y = 10, z = 15;
// Assigning values after declaration
age = 25;
name = "John Doe";
// Displaying variables
Console.WriteLine("Name: " + name);
Console.WriteLine("Age: " + age);
Console.WriteLine("Score: " + score);
Console.WriteLine("Price: " + price);
Console.WriteLine("Active: " + isActive);
Console.WriteLine("Grade: " + grade);
Console.WriteLine("x: " + x + ", y: " + y + ", z: " + z);
}
}
}
Output
Name: John Doe Age: 25 Score: 100 Price: 19.99 Active: True Grade: A x: 5, y: 10, z: 15
Variable Naming Rules
C# has specific rules for naming variables:
Example
using System;
namespace NamingRules
{
class Program
{
static void Main(string[] args)
{
// Valid variable names
string firstName = "John";
string _lastName = "Doe";
string address2 = "123 Main St";
string @class = "Math 101"; // @ allows using keywords as identifiers
// Invalid variable names (commented out to avoid errors)
// string 2ndAddress = "456 Oak St"; // Cannot start with digit
// string first-name = "John"; // Cannot contain hyphens
// string first name = "John"; // Cannot contain spaces
// string class = "Math"; // Cannot use reserved keywords
Console.WriteLine("First Name: " + firstName);
Console.WriteLine("Last Name: " + _lastName);
Console.WriteLine("Address: " + address2);
Console.WriteLine("Class: " + @class);
}
}
}
Output
First Name: John Last Name: Doe Address: 123 Main St Class: Math 101
Variable Scope
The scope of a variable determines where it can be accessed in your code:
Example
using System;
namespace VariableScope
{
class Program
{
// Class-level variable (field)
static string classLevelVariable = "I'm accessible throughout the class";
static void Main(string[] args)
{
// Method-level variable
string methodLevelVariable = "I'm accessible only in this method";
Console.WriteLine(classLevelVariable);
Console.WriteLine(methodLevelVariable);
// Block-level variable (inside if statement)
if (true)
{
string blockLevelVariable = "I'm accessible only in this block";
Console.WriteLine(blockLevelVariable);
// Can access method and class level variables
Console.WriteLine(methodLevelVariable);
Console.WriteLine(classLevelVariable);
}
// This would cause an error - blockLevelVariable is not accessible here
// Console.WriteLine(blockLevelVariable);
}
static void AnotherMethod()
{
// Can access class-level variable
Console.WriteLine(classLevelVariable);
// Cannot access method-level variables from other methods
// Console.WriteLine(methodLevelVariable);
}
}
}
Output
I'm accessible throughout the class I'm accessible only in this method I'm accessible only in this block I'm accessible only in this method I'm accessible throughout the class