AnyCompiler

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 karo ki bool capital letter ke saath True ya False print hota hai — ek chhota lekin genuinely C#-specific detail jo un logo 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);
    }
}
Output
30 19.99 False A

var type inference

var compiler ko ye allow karta hai ki wo variable ka type uske initializer se infer kar le. Lekin fir 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);
    }
}
Output
42 3.14159

Ek real type-mismatch error kaisa dikhta hai

C# ka compiler check karta hai ki har assignment ke types actually match karte hain — jahan int expected hai wahan string assign karna compile error hai, koi aisi cheez nahi jo baad mein runtime par pakdi 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);
    }
}
Output
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. Isse runtime ke jagah compile time par pakadna C# ki static typing ka ek main faayda hai.

Aage: operators aur control flow — if/else, for, while, aur switch.

Practice exercise

Ek variable temperature banao jiska type double ho aur value 72.5 ho, aur ek variable isSunny banao jiska type bool ho aur value true ho. Dono ko ek space se separate karke saath print karo.