Value types hold data directly. They include:int, float, double, char, bool, and decimal.
Reference types store a reference to the data. Includes:string, arrays, objects, and classes.
string name = "Charlie";
string[] fruits = { "apple", "banana" };int: whole numbersfloat: decimal (add f suffix)double: default for decimalsdecimal: high-precision, use m suffixStrings store sequences of characters and are reference types.
string greeting = "Hello, world!";
Console.WriteLine(greeting);var keyword allows C# to automatically determine the variable’s type based on the value you assign to it, making your code cleaner while still being type-safe.
Use var when the type is obvious from the value on the right-hand side. It improves code readability and reduces redundancy.
Comparison Table:
| Feature | var | Explicit Type |
|---|---|---|
| Type Declaration | Inferred by compiler | Manually declared by developer |
| Readability | Cleaner for complex types | More explicit and clear |
| Type Safety | ✔️ Compile-time checked | ✔️ Compile-time checked |
| Best Use Case | var result = new Dictionary<int, string>(); | Dictionary<int, string> result = new Dictionary<int, string>(); |
| When to Avoid | When the type is not obvious | Good for clarity and learning |
✅ Tip: Use var to reduce redundancy, but prefer explicit types when clarity is more important.
What will this print?
Ask the AI if you need help understanding or want to dive deeper in any topic