<aside> Variables are fundamental building blocks in programming that store and manage data. Think of them as labeled containers that hold different types of information.
</aside>
Variables in C# act as containers for data. Just like a labeled box, they store information that can be used and modified throughout your code.
<aside> 💡 Best Practice: Always name variables descriptively to indicate their purpose. For example, use 'customerAge' instead of just 'a'.
</aside>
// Basic variable declaration and assignment
string customerName = "John";
int age = 25;
bool isActive = true;
// Multiple declarations
int x = 10, y = 20, z = 30;
// Declaration now, assignment later
string status;
status = "Ready";
C# supports various mathematical operations for numerical calculations.
Operator | Description | Example |
---|---|---|
+= | Add and assign | x += 5; // Same as x = x + 5 |
-= | Subtract and assign | x -= 3; // Same as x = x - 3 |
*= | Multiply and assign | x *= 2; // Same as x = x * 2 |
/= | Divide and assign | x /= 4; // Same as x = x / 4 |
++ | Increment by 1 | x++; // Same as x = x + 1 |
-- | Decrement by 1 | x--; // Same as x = x - 1 |
int number = 10;
number += 5; // number is now 15
number *= 2; // number is now 30
number--; // number is now 29
// More complex calculations
int result = (number * 2) + (15 / 3); // Mathematical expression
double average = (value1 + value2) / 2.0; // Calculate average
int
for whole number calculations