C# Data Types

Value Types

Value types hold data directly. They include:int, float, double, char, bool, and decimal.

Loading...
Output:

Reference Types

Reference types store a reference to the data. Includes:string, arrays, objects, and classes.

string name = "Charlie";
string[] fruits = { "apple", "banana" };

Numeric Types

  • int: whole numbers
  • float: decimal (add f suffix)
  • double: default for decimals
  • decimal: high-precision, use m suffix
Loading...
Output:

Character and Boolean

Loading...
Output:

String Type

Strings store sequences of characters and are reference types.

string greeting = "Hello, world!";
Console.WriteLine(greeting);

Type Inference with var

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.

Loading...
Output:

var vs Explicit Type

Use var when the type is obvious from the value on the right-hand side. It improves code readability and reduces redundancy.

Comparison Table:

FeaturevarExplicit Type
Type DeclarationInferred by compilerManually declared by developer
ReadabilityCleaner for complex typesMore explicit and clear
Type Safety✔️ Compile-time checked✔️ Compile-time checked
Best Use Casevar result = new Dictionary<int, string>();Dictionary<int, string> result = new Dictionary<int, string>();
When to AvoidWhen the type is not obviousGood for clarity and learning

✅ Tip: Use var to reduce redundancy, but prefer explicit types when clarity is more important.

Practice Prompt

What will this print?

Loading...
Output:

Need Help?

Ask the AI if you need help understanding or want to dive deeper in any topic