Skip to content

7.2. C#

7.2.1. Snapshot

  • Install and package status: see Installation and Availability.
  • Target frameworks: netstandard2.0 and net8.0
  • Companion assemblies: VMx.Extensions.DependencyInjection (services.AddVMx(...)) and VMx.Notifications (opt-in INotificationHub); their NuGet pages appear with their first publication
  • Reactive primitive: System.Reactive
  • Naming idiom: PascalCase
  • Subclass access: a ComponentVMBase subclass, including one in another assembly, reads the injected dispatcher through the protected Dispatcher property (ADR-0229)
  • Hub delivery: see Callback-Safe Transition Ordering

7.2.2. What To Reach For

C# is the most direct fit when you want .NET host integration, ICommand-style UI binding, or desktop MVVM with WPF or Avalonia. The companion assemblies add dependency-injection registration and opt-in notifications.

7.2.3. Serviced Collections

ServicedObservableCollection<T> is an ObservableCollection<T> with normal local CollectionChanged delivery plus equivalent messages on an optional hub. Its complete surface includes inherited Add, Remove, RemoveAt, Move, Clear, the indexer, and named Replace / ReplaceAll:

var notes = new ServicedObservableCollection<Note>(hub);
notes.Add(first);
notes.Add(second);
notes.Replace(0, revised);
notes.Move(0, notes.Count - 1);    // one Move locally, then on the hub
notes.ReplaceAll(serverSnapshot); // one Reset, even for identical non-empty input

Invalid indices throw before mutation. Messages keep Index and add OldIndex / NewIndex. The Serviced Mutation Contract gives the shared rules for no-op cases, value removal, delivery order, and item ownership.

Choose ObservableList<T> instead for list-local batching and the Count channel. Choose a Group or Composite child collection for VM lifecycle ownership.

Add KeyedServicedObservableCollection<TKey,TItem> when that same ordered, caller-owned list also needs stable-key access:

var notesById = new KeyedServicedObservableCollection<Guid, Note>(
    note => note.Id,
    hub);
notesById.Add(first);
bool found = notesById.TryGetValue(first.Id, out Note? note);
bool added = notesById.Upsert(revised); // false: Replace at the same position
bool removed = notesById.RemoveKey(first.Id);

ContainsKey tests membership; an optional comparer follows the hub argument.

Mutating Id does not rekey the membership. The Keyed Serviced Mutation Contract gives the shared rules for captured keys, atomic failures, costs, and hub delivery.

7.2.4. Imperative Engine Bridge

SubscribeValue returns IDisposable and uses EqualityComparer<TValue>.Default unless an IEqualityComparer<TValue> is supplied:

using VMx.Messages;

IDisposable exposureSubscription = cameraVm.SubscribeValue(
    vm => vm.Model.Exposure,
    (exposure, _) => material.Uniforms.Exposure.Value = exposure,
    fireImmediately: true);

// Host adapter disposal:
exposureSubscription.Dispose();

Pass equalityComparer: for custom equality. The Imperative Selected-State Bridge gives the shared callback, reevaluation, and ownership rules.

7.2.5. Pointers

7.2.6. Current Example Coverage

  • Console: examples/csharp/console/HelloVMx/
  • WPF Todo app: examples/csharp/wpf/TodoApp/
  • Avalonia flagship: examples/csharp/avalonia/NotesShowcase/

Use the Smaller Examples page for the short demos and Integration Recipes for host wiring routes.