Frequently Asked Questions (FAQ)

Advanced Updated November 16, 2024

❓ FAQ: Expert-Level C# Questions

🔹 Q1: When should I use Span<T>?

Use Span<T> when working with slices of memory (like buffers) and you want high performance without allocations. It's commonly used in parsing, networking, or working with binary data.

🔹 Q2: What’s the difference between Expression<Func<T>> and Func<T>?

  • Func<T> represents executable code.
  • Expression<Func<T>> represents the structure of the code (abstract syntax tree), often used for building queries (e.g., in ORMs like Entity Framework).

🔹 Q3: Can C# generate code at runtime?

Yes. You can use Reflection.Emit for IL generation, or Expression Trees for runtime delegates. Source Generators offer compile-time code generation.

Only use unsafe when absolutely necessary for performance or interop (e.g., working with unmanaged code or native buffers). Always test for memory safety.

🔹 Q5: How is Dependency Injection better than static classes?

DI allows for testable, decoupled, flexible code, while static classes are rigid and difficult to mock or extend.