CSharp · Console
using System;
public class Program
{
public static void Main(string[] args)
{
B b = new B();
b.Run();
//Console.Read();
}
}
class A
{
public event Action onNameChanged;
private string _name;
public void SetName(string newName)
{
this._name = newName;
//The purpose of this method is NOT toonvoke this event.
//The invokation is a mere "Side effect"
if (onNameChanged != null)
onNameChanged();
}
public string GetName()
{
return this._name;
}
}
class B
{
A a ;
public void Run()
{
a = new A();
a.onNameChanged += a_onChanged;
a.SetName("John");
}
private void a_onChanged()
{
Console.WriteLine("The new name of 'a' is: "+ this.a.GetName());
}
}