Your country

Tools that support it use your country for local currency, number formats, units and paper size. Your choice is saved only in this browser.

Type a name or a two-letter code. Use the up and down arrow keys to move through the countries, Enter to choose one and Escape to close.

C# & .NET (C# 14, .NET 10 LTS) Module 12

Delegates, lambdas, events and functional style interview questions

Interview questions on delegates, lambdas, events and functional style: warm-ups, then what each harder question tests and how a good answer starts.

15 questions: 4 warm-ups and 11 with model answers · Levels: Fresher to staff

Warm-up questions

Short questions with short answers, each answered in full.

  1. Fresher

    What is a delegate in C#, and how is it different from an interface with one method?

    Answered in the lesson Delegates, Func and Action

  2. Fresher

    What is a closure in C#?

    Answered in the lesson Lambdas and closures

  3. Fresher

    What is the difference between a public delegate field and an event?

    Answered in the lesson Events, subscriptions and memory leaks

  4. Fresher

    What is a pure function, and why is it easier to test?

    Answered in the lesson Functional techniques in C#

Questions with model answers

What each question tests, the first step of a good answer and the follow-ups to expect.

  1. Mid to senior

    An order service combines three checks into one Func<Order, bool> with +=: stock, address and credit. Orders with a bad address still ship, and nothing logs an error. Explain what the combined delegate returns, why the bad orders pass, and how you would redesign the checks.

    What it tests
    What a multicast delegate returns, why that hides failed checks, what an exception in one target does, and a design that runs every check and reports every failure.
    First step
    Write down what calling the combined delegate does: each check runs in order with the same order, and only one bool comes back. Ask which check that is, and what happens to the results of the other two.
    Follow-ups

    From the lesson Delegates, Func and Action

    Model answer, Pro. Model answers open with any pass; prices are in the notice on this page.

    Opening the model answers with your pass…

    Model answers open with any MySmartCoPilot pass.

    • Pro pass · 1 week₹79$2.99
    • 30 days₹179$5.99
    • 1 year₹1,699$45

    One payment; a pass never renews. The price shown is the total you pay: no fees or charges are added at checkout.

    It also opens every other in-depth part in Learn and every Pro tool. Sales are final, except where the law gives you a right to withdraw, for example in the EU and the UK (refund policy).

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  2. Mid to senior

    A pricing loop runs 2 million times a minute and passes this.ApplyDiscount to a helper on every pass. A memory profile shows millions of short-lived Func<decimal, decimal> objects. Where do they come from, and how do you remove them without making the code harder to read?

    What it tests
    Method group conversions and when the compiler caches them, lambdas that capture this, measuring allocations, and choosing a fix that stays readable.
    First step
    Name the conversion: every pass turns the instance method group this.ApplyDiscount into a new delegate that holds the method and this. Measure the bytes with GC.GetAllocatedBytesForCurrentThread around the loop before you change anything.
    Follow-ups

    From the lesson Delegates, Func and Action

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  3. Fresher to mid

    A screen builds 4 seat buttons in a for loop and gives each a click lambda that books seats[i]. Every click throws IndexOutOfRangeException. Explain why, fix it, and say why the same code written with foreach works.

    What it tests
    Capture of variables rather than values, the single variable of a for loop, the new variable of each foreach pass, and fixes that keep each lambda's own value.
    First step
    Count the variables. The for loop declares i once, in its initializer, and every lambda captures that one i. Then ask what value i holds when a click finally runs, long after the loop has finished.
    Follow-ups

    From the lesson Lambdas and closures

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 2 minutes
  4. Mid to senior

    A dashboard keeps a small Func<string> title callback for every report it has shown. Memory grows by about 8 MB per report, although the callbacks only return titles, and a memory snapshot shows the report arrays reachable from the callbacks. Explain how, and fix it.

    What it tests
    How closures keep captured variables alive, what a kept delegate holds on to, and fixes that capture only what is needed.
    First step
    Open the method that made the callback and list every variable that any lambda in it captures, including lambdas that ran once and were thrown away. Then ask which object the kept callback refers to, and what that object holds.
    Follow-ups

    From the lesson Lambdas and closures

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  5. Mid to senior

    A team proposes a rule: every lambda on a hot path must be static. What does static prevent and what does it not, how do you pass a static lambda the data it needs, and would you adopt the rule?

    What it tests
    What a capture costs in allocations, what a static lambda guarantees, APIs that take a state argument, and when a coding rule like this pays for itself.
    First step
    Start from what a capture makes at run time, and when, and measure it with GC.GetAllocatedBytesForCurrentThread before arguing either way. Then say what static promises, and what it leaves out.
    Follow-ups

    From the lesson Lambdas and closures

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 2 minutes
  6. Mid to senior

    A payment service raises PaymentCompleted with five subscribers. An analytics handler starts throwing during an outage, and from then on the audit handler, subscribed after it, records no payments, although the payments succeed. Explain why, and redesign the raise without losing the analytics errors.

    What it tests
    Multicast invocation order, what an exception in one handler does to the others, isolating handlers with GetInvocationList, and deciding which handlers are independent.
    First step
    Write down the invocation list in order of subscription and mark where analytics sits. Raising the event calls the handlers one after another, and an exception that a handler does not catch leaves the raise at once.
    Follow-ups

    From the lesson Events, subscriptions and memory leaks

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 2 minutes
  7. Mid to senior

    A background thread raises StockChanged, and a screen removes its handler in Dispose on the UI thread. Now and then the screen's handler runs after Dispose and throws ObjectDisposedException. The raise uses ?.Invoke. Explain why that does not prevent it, and fix it.

    What it tests
    What ?.Invoke guarantees and what it does not, delegate snapshots during a raise, the race between a raise and an unsubscribe, and handlers that tolerate a late call.
    First step
    Separate two promises: what ?.Invoke guarantees about null, and what it does not guarantee about a handler that is removed while a raise is under way. Then line up the two threads step by step.
    Follow-ups

    From the lesson Events, subscriptions and memory leaks

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  8. Senior to staff

    A market-data service receives 20,000 price ticks a second and must feed a chart, a risk engine that sometimes pauses for half a second, and an archive that needs to know when the feed ends. Would you expose an event, IObservable<T>, IAsyncEnumerable<T> or a channel, and why?

    What it tests
    Matching push and pull delivery to consumers, back-pressure, end and error signals, and the cost of a slow consumer inside an event raise.
    First step
    Work out what a slow consumer does to each design: in an event, its handler runs inside the raise, so a half-second pause holds up 10,000 ticks for everyone. Then list which consumers need an end signal.
    Follow-ups

    From the lesson Events, subscriptions and memory leaks

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  9. Mid to senior

    The cart page shows Rs 679 for an order and the invoice, priced a moment later, shows Rs 656.10. Both call ApplyOffers(cart) on the same cart object. How do you work out why, and how do you redesign the pricing so that it cannot happen again?

    What it tests
    Recognising side effects, shared mutable state and hidden inputs, proving them with a test, and a pure redesign with immutable data and explicit inputs.
    First step
    Call ApplyOffers twice on one cart in a test and compare. If the totals differ, list everything the method reads and writes besides its result: the cart's lines, static fields, the clock, configuration.
    Follow-ups

    From the lesson Functional techniques in C#

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  10. Mid to senior

    A sign-up API throws ValidationException for every bad field. During a wave of bot sign-ups it handles 5,000 invalid requests a second and its CPU use and garbage collections climb. Would you switch to a Result type, and what changes for the callers and for the cost?

    What it tests
    The cost of exceptions for expected failures, measured; Result types and error collection; where exceptions stay right; migrating callers safely.
    First step
    Separate expected failures, such as a bad age, from unexpected ones, such as a lost database. Then measure what one expected failure costs as an exception against a returned value, in heap bytes per failure.
    Follow-ups

    From the lesson Functional techniques in C#

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 2 minutes
  11. Senior to staff

    A 3,000-line OrderService reads the clock, the database and configuration inside its pricing and shipping rules, and has almost no tests. How would you move it toward a functional core with an imperative shell, step by step, without stopping feature work?

    What it tests
    Finding side effects and hidden inputs, extracting pure functions safely, characterisation tests, and deciding how far to go before the cost outweighs the gain.
    First step
    Pick one rule that changes often, for example the shipping cut-off, and list what it reads. Write a test that pins today's behaviour, then move the rule into a pure method that takes those values as arguments.
    Follow-ups

    From the lesson Functional techniques in C#

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 2 minutes

All C# & .NET (C# 14, .NET 10 LTS) interview questions · The C# & .NET (C# 14, .NET 10 LTS) track

Quick answers and tool search

Type to search tools or to get a quick answer, for example 18% of 2500. Use the up and down arrow keys to move through the results, Enter to choose, and Escape to close.