C#

Fundamentals

6 question(s)

What is the difference between a variable declared with var and an explicit type?

Beginner
var uses compile-time type inference; the compiler still gives it a single static type. It is not dynamic — the type is fixed once inferred.
var count = 5;      // int
var name = "Sam";  // string
// count = "x"; // compile error
Real-world example Using var for obvious right-hand types (new lists, LINQ results) keeps code concise without losing type safety.

What is the difference between const and readonly?

Beginner
const is a compile-time constant embedded into callers and must be initialised inline; readonly is set once at runtime (declaration or constructor) and can vary per instance.
public const double Pi = 3.14159;
public readonly DateTime Created = DateTime.UtcNow;
Real-world example const for fixed math constants; readonly for values known only at construction, like an injected config value.

What is the difference between value and reference equality for strings?

Beginner
string overrides == and Equals to compare characters, so two strings with the same content are equal even if they are different objects.
var a = new string(new[]{'h','i'});
Console.WriteLine(a == "hi");            // True
Console.WriteLine(ReferenceEquals(a,"hi")); // False
Real-world example Comparing user input to an expected token by value, not reference.

What is the difference between checked and unchecked arithmetic?

Intermediate
By default integer overflow wraps silently (unchecked); a checked context throws OverflowException on overflow, which is safer for money/counters.
checked {
    int max = int.MaxValue;
    int y = max + 1; // throws OverflowException
}
Real-world example Wrapping financial calculations in checked so a silent overflow never corrupts a balance.

What is the difference between out, ref and in parameters?

Intermediate
ref passes a variable by reference (must be initialised, can be read/written); out must be assigned inside the method (need not be initialised by caller); in passes by reference read-only for performance.
bool TryParse(string s, out int value);
void Swap(ref int a, ref int b);
double Length(in Vector v); // read-only
Real-world example TryParse patterns use out to return both success and the parsed value.

How does string interpolation compile, and when should you avoid it?

Advanced
Interpolated strings usually compile to string.Format (or a DefaultInterpolatedStringHandler on modern .NET). Avoid it in hot logging paths where the message may not be emitted — use structured logging instead.
string s = $"User {id} at {DateTime.UtcNow}";
// logging: prefer logger.LogInformation("User {Id}", id);
Real-world example Structured logs let you query by the Id field later instead of parsing a formatted string.