public AddObserver ( IObserver, observer ) : void | ||
observer | IObserver, | |
return | void |
using System; using System.Collections.Generic; public class Subject { private ListIn this example, we create a Subject class that maintains a list of observers and provides methods for adding an observer and notifying all observers. The Observer class has a single method called Update which is called when the subject notifies all of its observers. We then create an instance of the Subject and an instance of the Observer, add the Observer to the Subject's list of observers, and notify all observers by calling the NotifyObservers method. It is difficult to determine the exact package library used in this example as the Observable pattern is a design pattern and not a specific package or library. However, it is common to use the System namespace in C# when implementing the Observable pattern._observers = new List (); public void AddObserver(Observer observer) { _observers.Add(observer); } public void NotifyObservers() { foreach (Observer observer in _observers) { observer.Update(); } } } public class Observer { public void Update() { Console.WriteLine("Observer updated"); } } class Program { static void Main(string[] args) { Subject subject = new Subject(); Observer observer = new Observer(); subject.AddObserver(observer); subject.NotifyObservers(); } }