I've found strange behavior in C#.
Look at the example below:
namespace ConsoleApplication1
{
class MyClass
{
public event Action<MyClass> Event;
public void OnEvent(MyClass arg) { }
public void OnEventObject(object arg) { }
}
class Program
{
static void Main(string[] args)
{
try
{
MyClass myClass = new MyClass();
myClass.Event += (Action<object>)myClass.OnEventObject; // Succeed.
myClass.Event += myClass.OnEvent; // ArgumentException: Delegates must be of the same type.
}
catch { Console.WriteLine("Section 1 failed!"); }
try
{
MyClass myClass = new MyClass();
myClass.Event += myClass.OnEvent; // Succeed.
myClass.Event += (Action<object>)myClass.OnEventObject; // ArgumentException: Delegates must be of the same type.
}
catch { Console.WriteLine("Section 2 failed!"); }
try
{
MyClass myClass = new MyClass();
myClass.Event += myClass.OnEvent; // Succeed.
myClass.Event += myClass.OnEventObject; // Succeed.
}
catch { Console.WriteLine("Section 3 failed!"); }
try
{
MyClass myClass = new MyClass();
myClass.Event += myClass.OnEventObject; // Succeed.
myClass.Event += myClass.OnEvent; // Succeed.
}
catch { Console.WriteLine("Section 4 failed!"); }
}
}
}
Section 1 and 2 Failed on the last Registration.
Section 2 and 3 Succeed.
Why does it happen?
I think the first section Is severe bug! Because the last registration event failed despite the signing method is compatible with hundred percent!