Variables aur Types
C# ke primitive types mein int, double, bool, aur char shamil hain — har variable ka ek declared type hota hai, aur declare hone ke baad wo type fixed rehta hai. Note karein ke bool capital letter ke sath True ya False print hota hai — ek chhota lekin genuinely C#-specific detail jo un logon ko confuse karta hai jo lowercase true/false print karne wali languages se aate hain.
using System;
class Program {
static void Main() {
int age = 30;
double price = 19.99;
bool isStudent = false;
char grade = 'A';
Console.WriteLine(age + " " + price + " " + isStudent + " " + grade);
}
}30 19.99 False A
var type inference
var compiler ko ye allow karta hai ke wo variable ka type us ke initializer se infer kar le. Lekin phir bhi compile time par ek fixed, fully static type banta hai — ye kisi scripting language jaisa dynamic type nahi hai.
using System;
class Program {
static void Main() {
var count = 42;
var pi = 3.14159;
Console.WriteLine(count + " " + pi);
}
}42 3.14159
Ek Real Type-Mismatch Error Kaisa Dikhta Hai
C# ka compiler check karta hai ke har assignment ke types waqai match karte hain — jahan int expected hai wahan string assign karna compile error hai, koi aisi cheez nahi jo baad mein runtime par pakri jaaye. Ye genuinely wahi hai jo compiler report karta hai — ek real run se capture kiya gaya, plausible dikhne ke liye likha nahi gaya:
using System;
class Program {
static void Main() {
int age = "thirty";
Console.WriteLine(age);
}
}Main.cs(5,19): error CS0029: Cannot implicitly convert type `string' to `int' Compilation failed: 1 error(s), 0 warnings
Kuch bhi run nahi hota — koi output nahi, sirf error. Is se runtime ki jagah compile time par pakarna C# ki static typing ka ek bara faida hai.
Aage: operators aur control flow — if/else, for, while, aur switch.
Practice exercise
Ek variable temperature banayein jiska type double ho aur value 72.5 ho, aur ek variable isSunny banayein jiska type bool ho aur value true ho. Dono ko ek space se separate kar ke sath print karein.