C# Programming
107 flashcards by MentisLibrary
C# for working developers: value and reference types, nullable references, classes interfaces and records, generics, the collection types, LINQ and deferred execution, async and await, exceptions, and the .NET runtime and tooling.
- What does var mean in C#?
- It asks the compiler to infer the variable's type from its initializer. The variable is still statically and permanently typed.
- How does string interpolation work?
- A string prefixed with $ may embed expressions in braces; the compiler turns them into a formatted string at run time.
- const vs readonly
- const is a compile-time constant baked into every call site and limited to primitives, strings and null. readonly is set once at run time, in the declaration or a constructor.
- What does a switch expression give you over a switch statement?
- It produces a VALUE rather than executing statements, requires every arm to be exhaustive or have a discard, and needs no break.
- What does the is pattern do?
- It tests the run-time type and, in the same expression, declares a typed variable that is in scope afterwards.
- What is the difference between ?? and ??=?
- ?? returns the left operand unless it is null, in which case it returns the right. ??= assigns the right operand only when the left is currently null.
- What does ?. do?
- It evaluates the member access only when the left side is non-null, otherwise the whole expression is null. The rest of the chain is short-circuited.
- What is the difference between out, ref and in parameters?
- ref passes an existing variable by reference for reading and writing. out also passes by reference but the callee must assign it. in passes by reference for reading only.
- When would you write a local function instead of a lambda?
- When the helper is only used inside one method and you want a name, recursion, ref or out parameters, or no delegate allocation.
- What does nameof return?
- The unqualified name of the symbol as a string, resolved at compile time, so renaming the symbol updates every use.
- What does the @ prefix do on a string literal?
- It makes the string verbatim: escape sequences are not processed and the literal may span lines, which suits file paths and regular expressions.
- How do you iterate a collection and why does foreach work?
- foreach calls GetEnumerator and then MoveNext and Current. Any type exposing that shape, usually through IEnumerable<T>, can be iterated.
- What is an expression-bodied member?
- A concise form for a member whose body is a single expression, written with => instead of braces and a return.
- How does an object initializer differ from a constructor call?
- It sets accessible properties after the constructor runs, in one expression, without needing a constructor overload for every combination.
- What is the core difference between a value type and a reference type?
- A value type variable holds the data itself and is copied on assignment. A reference type variable holds a reference, so assignment copies the reference and both names see the same object.
- When is a struct the right choice over a class?
- When the type is small, logically a single value, immutable, and short lived. Coordinates, money amounts and identifiers fit; anything with identity or inheritance does not.
- What is boxing, and why does it cost?
- Converting a value type to object or to an interface, which allocates a heap object holding a copy. Unboxing casts it back and copies again.
- What is a nullable value type?
- T? where T is a value type, which is Nullable<T>: a struct holding a value plus a HasValue flag, so an int can represent absence.
- What do nullable reference types actually change?
- Nothing at run time. With #nullable enable, the compiler tracks which references you declared as nullable and warns where a possibly-null value is dereferenced.
- Why is string immutable, and what follows from that?
- Every operation that appears to modify a string returns a new one. That makes strings safe to share across threads and usable as dictionary keys, at the cost of allocations.
- When should you reach for StringBuilder?
- When the number of concatenations is not known at compile time, typically a loop. It appends into a growable buffer and materializes the string once.
- What is an enum backed by, and what does that allow?
- An integral type, int by default. Any value of that integral type can be cast into the enum, whether or not it names a member.
- What does declaring a type as a record give you?
- Value-based equality, a readable ToString, deconstruction, and with expressions for non-destructive copies, all generated by the compiler.
- When is a tuple the right return type?
- For a private or local multi-value result where naming a type would add noise. Tuple elements can be named, and the result deconstructs at the call site.
- Which members does every type inherit from object?
- ToString, Equals, GetHashCode and GetType. Overriding Equals without GetHashCode breaks every hash-based collection.
- When must you use decimal instead of double?
- For money and any base-10 exact arithmetic. decimal is a 128-bit base-10 type, so 0.1 is exact; double is binary floating point and cannot represent it exactly.
- Is integer overflow checked in C# by default?
- No, arithmetic wraps silently. A checked block or expression makes overflow throw OverflowException instead.
- What does dynamic change about a member call?
- Type checking is deferred to run time. The compiler emits a call site that resolves the member when it executes, so a typo becomes a RuntimeBinderException.
- What is an auto-implemented property?
- A property whose backing field the compiler generates. { get; set; } is read and write, { get; } is set only in the constructor, and { get; init; } is set only during initialization.
- What can an interface declare, and what can it not hold?
- Members a type promises to provide: methods, properties, events and indexers. It holds no instance state, and a class may implement many interfaces.