Variables, Data Types, and Operators in C# 🚀
Understanding variables, data types, and operators is essential for writing C# programs. Let’s go step by step.
1️⃣ Variables in C#
A variable is a named storage location that holds data.
🔹 Declaring Variables
dataType variableName = value;
Example:
int age = 30;
string name = "John";
double price = 99.99;
🔹 Variable Naming Rules
- ✅ Must start with a letter or underscore (
_) - ✅ Can contain letters, digits, and underscores
- ✅ Case-sensitive (
myVarandMyVarare different) - ✅ Cannot use C# keywords (e.g.,
int,class,public)
2️⃣ Data Types in C#
C# has value types (stored directly in memory) and reference types (stored as references).
🔹 Common Value Types
| Data Type | Size | Example | Description |
|---|---|---|---|
int | 4 bytes | int x = 100; | Stores whole numbers |
double | 8 bytes | double pi = 3.14; | Stores decimal numbers |
float | 4 bytes | float price = 9.99f; | Smaller decimal precision |
char | 2 bytes | char grade = 'A'; | Stores a single character |
bool | 1 byte | bool isActive = true; | Stores true or false |
decimal | 16 bytes | decimal salary = 10000.50m; | High-precision financial calculations |
long | 8 bytes | long bigNumber = 1000000000L; | Stores large integers |
short | 2 bytes | short smallNum = 300; | Stores small whole numbers |
🔹 Reference Types
| Data Type | Example | Description |
|---|---|---|
string | string message = "Hello"; | Stores text |
object | object obj = 42; | Can store any data type |
dynamic | dynamic data = "text"; | Type can change at runtime |
3️⃣ Implicit and Explicit Type Conversion
🔹 Implicit Conversion (Safe)
int num = 10;
double result = num; // Implicit conversion (int → double)
🔹 Explicit Conversion (Casting)
double price = 99.99;
int roundedPrice = (int) price; // Explicit conversion (double → int)
🔹 Using Convert Class
string strNumber = "123";
int number = Convert.ToInt32(strNumber);
4️⃣ Operators in C#
Operators perform operations on variables and values.
🔹 Arithmetic Operators
| Operator | Example | Description |
|---|---|---|
+ | a + b | Addition |
- | a - b | Subtraction |
* | a * b | Multiplication |
/ | a / b | Division |
% | a % b | Modulus (remainder) |
int a = 10, b = 5;
Console.WriteLine(a + b); // 15
Console.WriteLine(a - b); // 5
Console.WriteLine(a * b); // 50
Console.WriteLine(a / b); // 2
Console.WriteLine(a % b); // 0
🔹 Relational (Comparison) Operators
| Operator | Example | Description |
|---|---|---|
== | a == b | Equal to |
!= | a != b | Not equal to |
> | a > b | Greater than |
< | a < b | Less than |
>= | a >= b | Greater than or equal to |
<= | a <= b | Less than or equal to |
int x = 10, y = 5;
Console.WriteLine(x > y); // True
Console.WriteLine(x == y); // False
Console.WriteLine(x != y); // True
🔹 Logical Operators
| Operator | Example | Description |
|---|---|---|
&& | a && b | Logical AND |
|| | a || b | Logical OR |
! | !a | Logical NOT |
bool isSunny = true;
bool isWeekend = false;
Console.WriteLine(isSunny && isWeekend); // False
Console.WriteLine(isSunny || isWeekend); // True
Console.WriteLine(!isSunny); // False
🔹 Assignment Operators
| Operator | Example | Equivalent To |
|---|---|---|
= | a = b | Assign b to a |
+= | a += b | a = a + b |
-=
| a -= b | a = a - b |
*= | a *= b | a = a * b |
/= | a /= b | a = a / b |
%= | a %= b | a = a % b |
int num = 10;
num += 5; // num = num + 5 (15)
num *= 2; // num = num * 2 (30)
🔹 Unary Operators
| Operator | Example | Description |
|---|---|---|
++ | a++ | Increment |
-- | a-- | Decrement |
int count = 5;
Console.WriteLine(count++); // 5 (Post-increment)
Console.WriteLine(++count); // 7 (Pre-increment)
5️⃣ Constants (const and readonly)
🔹 const (Compile-time constant) – Value must be assigned at declaration and cannot change.
const double PI = 3.14159;
🔹 readonly (Run-time constant) – Value can be assigned in constructor but cannot change afterward.
class Example {
readonly int number;
public Example() {
number = 100; // Allowed
}
}
🔚 Summary
- ✅ Variables store values and follow naming rules
- ✅ Data types include value types (
int,double) and reference types (string,object) - ✅ Implicit and explicit conversions help change data types
- ✅ Operators include arithmetic, relational, logical, and assignment operators
Comments
Post a Comment