Module 1 · Modern C#, Async and ASP.NET Core · Lesson 1 of 10
Modern C# language features
Practice method
Answer aloud first, then compare with the explanation and example. The interview tip is the production-grade detail to mention after the basic answer.
Fresh Interview Q&A - 2 / 43
C# / .NET Fresh Questions
Modern C# language features not covered deeply before
Question 1. What are collection expressions in C#?
Answer: Collection expressions use square brackets to create arrays, spans, lists, and other collection-like targets with one compact syntax. They are not LINQ queries; the elements are evaluated and a concrete collection is created for the target type.
Example:
int[] ids = [1, 2, 3]; List<string> names = ["Raj", "Anita"]; ReadOnlySpan<char> vowels = ['a', 'e', 'i', 'o', 'u'];
Interview tip: Good answer: mention that [] is target-typed. When the target type is unclear, add an explicit type or cast.
Question 2. What is the spread element in a collection expression?
Answer: The spread element copies items from another collection into the new collection expression. It is useful for combining small sequences without manual AddRange calls.
Example:
int[] first = [1, 2]; int[] second = [3, 4]; int[] all = [0, ..first, ..second, 5]; // 0,1,2,3,4,5
Interview tip: Do not confuse spread with lazy concatenation. The target collection is still created.
Question 3. What is target-typed new?
Answer: Target-typed new lets the compiler infer the type from the left side or parameter type. It reduces repetition, but overuse can make code less readable when the target type is far away.
Example:
Dictionary<int, string> cache = new();
Order order = new(id: 10, total: 499.99m);
CreateCustomer(new("Raj", "Pune")); // type inferred from parameterInterview tip: Use it where the target type is obvious. Avoid it in complex expressions where the reader must hunt for the type.
Question 4. What is a with-expression?
Answer: A with-expression creates a copy of a record, record struct, or suitable struct and changes selected properties. It is commonly used for immutable models and request/response DTOs.
Example:
public record Invoice(int Id, decimal Total, string Status);
var draft = new Invoice(1, 1000m, "Draft");
var approved = draft with { Status = "Approved" };Interview tip: Mention non-destructive mutation: the original object is not changed.
Question 5. What are extension blocks in C# 14?
Answer: Extension blocks are newer syntax for grouping extension members for a type. They can define extension methods, extension properties, and static extension members. The older this-parameter extension method syntax still works.
Example:
public static class StringExtensions
{
extension(string value)
{
public bool IsBlank => string.IsNullOrWhiteSpace(value);
}
}
bool missing = " ".IsBlank;Interview tip: Interview angle: extension members do not break encapsulation; they can only access public/internal members available to normal code.
Question 6. What is the C# field keyword in properties?
Answer: The field keyword lets a property accessor refer to the compiler-generated backing field without manually declaring a private field. It is useful for validation in auto-property-like code.
Example:
public string Name
{
get;
set => field = string.IsNullOrWhiteSpace(value)
? throw new ArgumentException("Name is required")
: value;
} = "Unknown";Interview tip: Useful correction: field is contextual. If you already have a member named field, use @field when you mean that member.
Question 7. What is a file-local type?
Answer: A file-local type is visible only inside the source file where it is declared. It is useful for source generators or helper types that should not leak into the rest of the assembly.
Example:
file sealed class CsvToken
{
public required string Value { get; init; }
}Interview tip: This is different from internal. internal is visible across the assembly; file is limited to one source file.
Question 8. What are partial classes and partial methods?
Answer: partial lets a type or member be split across files and combined at compile time. It is common in generated code, UI designer code, and source-generator scenarios.
Example:
// Customer.g.cs - generated
public partial class Customer
{
partial void OnCreated();
}
// Customer.cs - handwritten
public partial class Customer
{
partial void OnCreated() => Console.WriteLine("created");
}Interview tip: Keep business logic in handwritten files. Do not edit generated files unless the tool expects it.
Question 9. What is explicit interface implementation?
Answer: Explicit interface implementation implements an interface member so it is accessible only through the interface reference. It avoids public API pollution and resolves member-name conflicts.
Example:
public sealed class Report : IDisposable
{
void IDisposable.Dispose()
{
Console.WriteLine("Disposed through IDisposable");
}
}
IDisposable disposable = new Report();
disposable.Dispose();Interview tip: Use it when a member is part of a contract but should not be a normal public method on the class.
Question 10. What is using static?
Answer: using static imports static members of a type so they can be called without qualifying the type name. It is common for Math, assertion APIs, or test helper methods.
Example:
using static System.Math; double distance = Sqrt(Pow(x2 - x1, 2) + Pow(y2 - y1, 2));
Interview tip: Use sparingly. Too many using static imports can hide where methods come from.
Question 11. What are CallerMemberName and CallerArgumentExpression used for?
Answer: Caller info attributes let the compiler pass source information to a method automatically. CallerMemberName is useful for logging and property change notification. CallerArgumentExpression is useful for guard clauses that report the failed expression.
Example:
public static void NotNull<T>(
T? value,
[System.Runtime.CompilerServices.CallerArgumentExpression("value")] string? expression = null)
{
if (value is null) throw new ArgumentNullException(expression);
}
NotNull(order.Customer);Interview tip: This avoids hard-coded parameter names and gives better exception messages.
Question 12. What are static abstract members in interfaces?
Answer: Static abstract interface members let an interface require static members such as operators or factory methods. They enable generic algorithms over numeric-like types.
Example:
public interface IAddable<TSelf>
where TSelf : IAddable<TSelf>
{
static abstract TSelf operator +(TSelf left, TSelf right);
}
static T Sum<T>(T a, T b) where T : IAddable<T> => a + b;Interview tip: This is the language foundation behind many generic math APIs.
Question 13. What is generic math in .NET?
Answer: Generic math lets you write numeric algorithms once and use them for int, decimal, double, and custom numeric types that implement the required numeric interfaces.
Example:
using System.Numerics;
static T Average<T>(T a, T b) where T : INumber<T>
{
return (a + b) / T.CreateChecked(2);
}Interview tip: Mention the tradeoff: it is powerful for libraries, but ordinary business code often does not need it.
Question 14. What do unmanaged, notnull, and default generic constraints mean?
Answer: unmanaged restricts T to types that contain no object references. notnull disallows nullable reference or nullable value types. default is used in overrides or explicit interface implementations to match an unconstrained base generic method.
Example:
static int SizeOfValue<T>() where T : unmanaged
{
return System.Runtime.InteropServices.Marshal.SizeOf<T>();
}
static Dictionary<TKey, string> Create<TKey>() where TKey : notnull
=> new();Interview tip: Constraints are about what the generic code is allowed to do safely at compile time.
Question 15. What does scoped mean in ref-safe code?
Answer: scoped tells the compiler that a reference is not allowed to escape the current method or scope. It helps prevent dangling references when using ref structs, spans, and ref returns.
Example:
static void Print(scoped ReadOnlySpan<char> text)
{
Console.WriteLine(text.ToString());
// Cannot safely store text for later use.
}Interview tip: Most app developers rarely write scoped manually, but it matters for high-performance APIs using spans.
Question 16. What are readonly members in structs?
Answer: readonly members promise not to mutate the struct instance. They can reduce defensive copies and communicate immutability intent for value types.
public struct Point
{
public double X { get; init; }
public double Y { get; init; }
public readonly double Length => Math.Sqrt(X * X + Y * Y);
}Interview tip: For small value objects, prefer readonly struct or readonly members to avoid accidental mutation surprises.