Observable design choices
Observable design choices
Use a pull API when callers need to ask for current data; use an event when they need notice of a change. The .NET observer interfaces additionally represent a push stream with explicit subscription ownership and terminal notifications. None of these contracts alone stores a durable history.
Examples target the proposed C# 14 / .NET 10 console baseline. Each is a separate Program.cs; expected outputs are reasoned, not executed.
Pull current state
A property or method provides a snapshot when asked. With an enumerable, the consumer chooses when to request items. A caller that only needs the latest count can avoid managing a subscription.
using System;var stock = new Stock();stock.Quantity = 4;Console.WriteLine(stock.Quantity);stock.Quantity = 9;Console.WriteLine(stock.Quantity);class Stock{ public int Quantity { get; set; }}The caller reads two values explicitly. No callbacks occur when the property changes.
Expected output (reasoned, not executed):
49Push an update
Events notify only attached handlers at the time of a normal invocation. They need not replay earlier changes to late subscribers. Use a separate state query for the current value when needed.
using System;var gauge = new Gauge();gauge.Set(2);gauge.Changed += (_, value) => Console.WriteLine($"notice {value}");Console.WriteLine($"current {gauge.Value}");gauge.Set(5);class Gauge{ public int Value { get; private set; } public event EventHandler<int>? Changed; public void Set(int value) { Value = value; Changed?.Invoke(this, value); }}The earlier update was not replayed. A state query reads 2, then the new notification delivers 5.
Expected output (reasoned, not executed):
current 2notice 5Recognize the observer contract
IObservable<T>.Subscribe returns an IDisposable subscription. IObserver<T> receives OnNext values, OnError for failure or OnCompleted for successful termination. The two terminal outcomes are alternatives. This finite, synchronous source sends values during Subscribe and completes before returning; it owns no ongoing resources. Real asynchronous sources need lifetime and concurrency rules.
using System;var source = new FixedFeed();using var subscription = source.Subscribe(new Printer());class FixedFeed : IObservable<int>{ public IDisposable Subscribe(IObserver<int> observer) { ArgumentNullException.ThrowIfNull(observer); observer.OnNext(4); observer.OnNext(7); observer.OnCompleted(); return new Finished(); } private sealed class Finished : IDisposable { public void Dispose() { } }}class Printer : IObserver<int>{ public void OnNext(int value) => Console.WriteLine(value); public void OnError(Exception error) => Console.WriteLine("failed"); public void OnCompleted() => Console.WriteLine("done");}The observer receives two values and completion before Subscribe returns. Disposing this already-finished token has no effect; it is not a template for an ongoing subscription.
Expected output (reasoned, not executed):
47doneCommon mistakes and edge cases
An observer contract does not automatically buffer values, provide backpressure, switch threads, or retry. A message bus may have different routing and durability; an ordinary local event is not such a bus. Decide who controls the pace, whether late readers need replay, how errors are delivered, and how the relationship ends. An observer callback that throws is not automatically a source OnError notification.
Practice and recap
Design an in-memory status display that opens after an initial update. Show how to display the current value immediately, receive future changes, and stop receiving changes when closed. Assume one thread.
Hint: Query the state once, subscribe, and store the handler for removal.
Learning objective: Compare events with pull-based enumeration. See the accompanying manual practice and knowledge check.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Observable design choices — knowledge check
Apply the lesson concepts to the following scenarios.
Which approach fits a caller that only needs current stock once?
Does the Gauge replay the value 2 to its late subscriber?
What does IObservable<T>.Subscribe return?
Which observer callback signals successful completion?
Why is FixedFeed disposal a no-op?