Difference in invoking through Delegates and Events C#

后端 未结 5 900
天命终不由人
天命终不由人 2021-01-13 22:11

What is the difference?

Using Delegate

public delegate void TestDelegate();
public TestDelegate delObj = SomeMethod;

public void S         


        
5条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-13 22:49

    It is not delegate vs event, an event is a kind-of property around a delegate.

    In general you should always use the second scenario because in the first public TestDelegate delObj is a public field, breaking Encapsulation.

    You use events in the interface of a class, delegates as parameters to methods.

提交回复
热议问题