Variables are used to store data that can be used and manipulated throughout a program. Each variable has a type that determines what kind of data it can hold, such as integers, floating-point numbers, characters, strings, and more. Below, I'll provide an overview of variables in C#, including their declaration, initialization, and examples.
Variable Declaration and Initialization
- Declaration: This is the process of defining a variable's name and type.
- Initialization: This is the process of assigning a value to the variable.
Basic Data Types in C#
Here are some common data types in C#:
- int: Represents a 32-bit signed integer.
- float: Represents a single-precision floating-point number.
- double: Represents a double-precision floating-point number.
- char: Represents a single 16-bit Unicode character.
- string: Represents a sequence of characters.
- bool: Represents a Boolean value (true or false).
Example Code
Here’s a simple example that demonstrates the declaration and initialization of various variable types:
csharp1using System; 2 3class Program 4{ 5 static void Main() 6 { 7 // Integer variable 8 int age = 30; 9 Console.WriteLine("Age: " + age); 10 11 // Floating-point variable 12 float height = 5.9f; // Note the 'f' suffix for float 13 Console.WriteLine("Height: " + height); 14 15 // Double variable 16 double weight = 70.5; // No suffix needed for double 17 Console.WriteLine("Weight: " + weight); 18 19 // Character variable 20 char initial = 'A'; 21 Console.WriteLine("Initial: " + initial); 22 23 // String variable 24 string name = "John Doe"; 25 Console.WriteLine("Name: " + name); 26 27 // Boolean variable 28 bool isStudent = false; 29 Console.WriteLine("Is Student: " + isStudent); 30 } 31}
Variable Naming Rules
When naming variables in C#, keep the following rules in mind:
- Variable names must start with a letter or an underscore (_).
- They can contain letters, digits, and underscores.
- Variable names cannot contain spaces or special characters (like @, #, $, etc.).
- Variable names are case-sensitive (e.g.,
myVar
andMyVar
are different). - Avoid using reserved keywords (like
int
,class
,void
, etc.) as variable names.
Example of Variable Naming
csharp1int score; // Valid 2float _temperature; // Valid 3string userName; // Valid 4bool isActive; // Valid 5 6// Invalid examples: 7// int 1stPlace; // Invalid: cannot start with a digit 8// string first name; // Invalid: cannot contain spaces 9// float @value; // Invalid: cannot use reserved keywords
Constants
In C#, you can define constants using the const
keyword. Constants are variables whose values cannot be changed once they are initialized.
csharp1const double Pi = 3.14159; 2Console.WriteLine("Value of Pi: " + Pi);
Using Variables in Operations
You can perform various operations using variables, such as arithmetic operations, string concatenation, and logical operations.
csharp1int a = 10; 2int b = 20; 3int sum = a + b; // Arithmetic operation 4Console.WriteLine("Sum: " + sum); 5 6string firstName = "John"; 7string lastName = "Doe"; 8string fullName = firstName + " " + lastName; // String concatenation 9Console.WriteLine("Full Name: " + fullName); 10 11bool isAdult = age >= 18; // Logical operation 12Console.WriteLine("Is Adult: " + isAdult);