Delegate Equality C# with Example



Delegate Equality C# with Example

Calling .Equals() on a delegate compares by reference equality: 
Action action1 = () => Console.WriteLine("Hello delegates"); 
Action action2 = () => Console.WriteLine("Hello delegates"); 
Action action1Again = action1; 
Console.WriteLine(action1.Equals(action1)) // True 
Console.WriteLine(action1.Equals(action2)) // False 
Console.WriteLine(action1Again.Equals(action1)) // True 
These rules also apply when doing += or -= on a multicast delegate, for example when subscribing and 
unsubscribing from events. 

0 Comment's

Comment Form

Submit Comment