📁 Sangam Dhakal - Portfolio v2.0 - Netscape Navigator 4.0
_
□
×

πŸ“š .NET Interview Questions

377 questions for senior .NET engineer interviews, each with a 30-second answer, the full answer, a deep dive, the follow-ups interviewers ask next, and real-world production problems.

Phase 2 — C# Fundamentals

The C# language core for .NET 10 and C# 14: language basics, the type system, strings, arrays, collections and exception handling, with the internals and trade-offs senior interviews probe.

  1. How does string interpolation work in C#? Explain interpolated string handlers, FormattableString, raw interpolated strings, which culture is used, and where interpolation is dangerous (SQL and logging).

    String interpolation in C# 14: what $"..." compiles to, interpolated string handlers and their performance, alignment and format specifiers, culture rules, raw interpolated literals, and the SQL-injection and structured-logging traps.

    Mid-Level Theory Strings Β· πŸ”— 2 follow-ups Β· πŸ›  1 real-world
  2. How does string formatting work in .NET? Explain composite formatting, standard and custom format strings for numbers and dates, IFormatProvider and CultureInfo, and when to use the invariant culture.

    String formatting in .NET 10: composite format items, standard and custom numeric and date format strings, IFormatProvider and CultureInfo (including en-AU), parsing with cultures, invariant globalisation mode, and span-based TryFormat.

    Junior Theory Strings Β· πŸ”— 2 follow-ups
  3. What is the difference between ordinal and culture-sensitive string comparison in .NET? When should you use StringComparison.Ordinal, OrdinalIgnoreCase, CurrentCulture and InvariantCulture, and which string methods default to which?

    Ordinal versus linguistic string comparison in .NET 10: how each works, which methods default to culture-sensitive comparison, case-insensitive comparison done right, the Turkish I problem, comparers for collections, and security implications.

    Mid-Level Theory Strings Β· πŸ”— 2 follow-ups Β· πŸ›  1 real-world
  4. After moving a .NET service from Windows (.NET Framework) to Linux containers on .NET 10, string sorting, IndexOf results and some culture formatting changed, and one image failed to start with a culture error. Why, and how do you diagnose and fix culture-sensitive behaviour?

    A real-world culture-sensitive comparison scenario: NLS versus ICU, invariant globalisation mode in slim container images, IndexOf and sort-order changes, time zone ids, and how to make string behaviour deterministic across platforms.

    Senior RealWorldScenario Strings Β· πŸ”— 2 follow-ups Β· πŸ›  1 real-world
  5. How do you work with text encodings in C#? Explain the Encoding class, UTF-8 with and without a BOM, legacy code pages, u8 literals, Rune, grapheme clusters and Unicode normalisation.

    Practical encoding work in .NET 10: Encoding APIs and defaults, BOMs, invalid-byte handling, Windows-1252 via CodePagesEncodingProvider, UTF-8 literals and the Utf8 class, Rune and StringInfo for user-visible characters, and normalisation for comparisons.

    Mid-Level Theory Strings Β· πŸ”— 2 follow-ups Β· πŸ›  1 real-world
  6. What is ReadOnlySpan<char>, and how does it let you parse and slice strings without allocating? Show a parser that uses it, and explain its limitations and when to use ReadOnlyMemory<char> instead.

    ReadOnlySpan<char> for zero-allocation text processing in .NET 10: slicing, span-based parsing and searching, splitting without substrings, stackalloc and ArrayPool buffers, alternate dictionary lookups, ref struct restrictions and ReadOnlyMemory.

    Senior Coding Strings Β· πŸ”— 2 follow-ups Β· πŸ›  1 real-world
  7. Code review: an export method builds a cache key with customer.Name.ToLower() + "_" + date.ToString(), checks status.ToUpper() == "ACTIVE", validates emails with IndexOf("@") > 0, appends CSV rows with result += line, truncates names with Substring(0, 50), calls name.Trim() without assigning it, and logs with $"Exported {count} rows". What is wrong and how would you rewrite it?

    A string-handling code review that combines the Phase 2 string topics: immutability, comparison, culture, formatting, concatenation, truncation, null handling and logging, with a rewritten version.

    Senior CodeReview Strings Β· πŸ”— 2 follow-ups Β· πŸ›  1 real-world
  8. How do single-dimensional arrays work in C#? How do you declare, initialise, copy, resize, slice and search them, and when would you use an array instead of List<T>?

    Single-dimensional arrays in C# 14: declaration and collection expressions, fixed length and zero-initialisation, the Array helper methods, copying versus slicing, ranges that copy, and choosing between arrays and List<T>.

    Junior Theory Arrays Β· πŸ”— 3 follow-ups
  9. What is the difference between multi-dimensional (rectangular) arrays and jagged arrays in C#? How are they laid out in memory, which is faster, and when would you flatten a grid into a single-dimensional array?

    Rectangular int[,] versus jagged int[][] arrays: syntax, memory layout, bounds checks and performance, LINQ and Span support, and the flattened 1-D array alternative for grids, images and matrices.

    Mid-Level Theory Arrays Β· πŸ”— 2 follow-ups Β· πŸ›  1 real-world
  10. This code compiles but throws ArrayTypeMismatchException at run time: object[] items = new string[3]; items[0] = 42;. Explain array covariance in C#, why it exists, what it costs, and how generic variance and read-only interfaces avoid the problem.

    Array covariance in .NET: why string[] converts to object[], the run-time store check and ArrayTypeMismatchException, its hidden performance cost, the Span<T> constructor trap, and safe alternatives through generic variance.

    Mid-Level Debugging Arrays Β· πŸ”— 2 follow-ups Β· πŸ›  1 real-world
  11. What is bounds checking in .NET arrays? Why does it exist, when does the JIT eliminate it, and how do you write loops that avoid it safely (and when is unsafe code justified)?

    Array bounds checking in .NET 10: the check the JIT emits, why it gives memory safety, patterns that let the JIT remove it, patterns that keep it, how to inspect generated code, and the trade-offs of unsafe access.

    Senior Theory Arrays Β· πŸ”— 2 follow-ups Β· πŸ›  1 real-world
  12. A .NET service that processes large batches spends a lot of time in garbage collection, and memory profiling shows most allocations are short-lived arrays. How do you make array-heavy code fast? Explain ArrayPool, the Large Object Heap, cache locality, copying APIs, spans and SIMD.

    Array performance in .NET 10 as a real-world scenario: allocation and LOH costs, ArrayPool rent/return rules, uninitialised and pinned arrays, cache-friendly layouts, vectorised copy and fill APIs, TensorPrimitives and SIMD, and how to measure.

    Senior RealWorldScenario Arrays Β· πŸ”— 2 follow-ups Β· πŸ›  1 real-world
  13. What is the difference between ArrayList and List<T> in C#? Why is ArrayList still in .NET, and what goes wrong when you use it in new code?

    Compares the non-generic ArrayList with the generic `List<T>`: type safety, boxing, performance and when you still meet ArrayList.

    Junior Theory Collections Β· πŸ”— 2 follow-ups
  14. How does List<T> work internally? Explain capacity and growth, the cost of Add, Insert and Remove, why modifying a list during foreach throws, and how CollectionsMarshal.AsSpan and EnsureCapacity help in hot paths.

    Internals of `List<T>`: backing array, doubling growth, operation costs, version checks, and modern performance APIs.

    Mid-Level Theory Collections Β· πŸ”— 3 follow-ups Β· πŸ›  1 real-world
  15. How do you use Dictionary<TKey,TValue> correctly in C#? Explain TryGetValue versus ContainsKey plus the indexer, Add versus the indexer setter, TryAdd and GetValueOrDefault, custom comparers, CollectionsMarshal.GetValueRefOrAddDefault, and whether enumeration order is guaranteed.

    Practical `Dictionary<TKey,TValue>` API usage: single-lookup patterns, insert semantics, comparers, ref access and ordering.

    Mid-Level Coding Collections Β· πŸ”— 3 follow-ups Β· πŸ›  1 real-world
  16. How do you use HashSet<T> in C#? Explain Add returning bool, the set operations (UnionWith, IntersectWith, ExceptWith, SetEquals), custom comparers, and why HashSet.Contains beats List.Contains. When would you use SortedSet<T> or FrozenSet<T> instead?

    The `HashSet<T>` API in practice: uniqueness, set algebra, comparers, and the sorted and frozen alternatives.

    Junior Theory Collections Β· πŸ”— 2 follow-ups
  17. How do Queue<T> and Stack<T> work in C#? Explain their internal storage, Enqueue/Dequeue and Push/Pop, the Try methods, the enumeration order, and when you would use PriorityQueue<TElement,TPriority> or a concurrent queue instead.

    The .NET `Queue<T>` and `Stack<T>` APIs, their circular-buffer and array internals, and the priority and concurrent alternatives.

    Junior Theory Collections Β· πŸ”— 3 follow-ups
  18. What is the difference between SortedDictionary<TKey,TValue> and SortedList<TKey,TValue> in C#? How is each implemented, what are the costs of insert, lookup and enumeration, and how do you choose between them (and versus Dictionary)?

    Compares the two sorted key-value collections in .NET: red-black tree versus sorted arrays, costs, memory and use cases.

    Mid-Level Theory Collections Β· πŸ”— 2 follow-ups Β· πŸ›  1 real-world
  19. When would you use LinkedList<T> in C#? Explain how it is implemented, the LinkedListNode<T> API (AddBefore, AddAfter, Remove(node)), its real costs compared with List<T>, and the classic LRU cache that combines it with a Dictionary.

    The .NET doubly linked list: node API, O(1) operations given a node, memory and cache costs, and the LRU cache pattern.

    Mid-Level Coding Collections Β· πŸ”— 3 follow-ups Β· πŸ›  1 real-world
  20. What are immutable collections in .NET? Compare ImmutableArray<T>, ImmutableList<T> and ImmutableDictionary<TKey,TValue>, explain builders and structural sharing, and contrast them with ReadOnlyCollection<T>, IReadOnlyList<T> and FrozenDictionary.

    System.Collections.Immutable types, their internal structures and costs, builders, and how they differ from read-only views and frozen collections.

    Mid-Level Theory Collections Β· πŸ”— 2 follow-ups Β· πŸ›  1 real-world

🌐 Sangam's Webring

βͺ Previous 🏠 Main Next ⏩
πŸ’¬ Chat with me