Why does the following code not print "Hello, World!"?
using System;
namespace Test
{
public class Program
{
public static void Main(string[] args)
{
var a = new A();
var b = new B(a);
b.Evnt += val => Console.WriteLine(val);
a.Do();
}
}
public class A
{
public void Do()
{
Evnt("Hello, World!");
}
public event Action<string> Evnt = v => {};
}
public class B
{
public B(A a)
{
a.Evnt += Evnt; // this does not work
}
public event Action<string> Evnt = v => {};
}
}
But if I replace line
a.Evnt += Evnt;
with
a.Evnt += v => Evnt(v);
everything works fine.
If this is forbidden to do so, what meaning has subscribing one event to another, and why are there no compilation errors or warnings when doing so?