Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I put a delegate in an interface?

Why can't I add a delegate to my interface?

like image 744
user73936 Avatar asked Mar 04 '09 23:03

user73936


People also ask

Can we declare delegate in interface?

It's the base of design in the Object Oriented Programming. These are basically declaring delegate types, so they don't belong in an Interface. You can use Event in Interface with Delegate type.

Can we declare delegate inside interface and why?

A Delegate is just another type, so you don't gain anything by putting it inside the interface. You shouldn't need to create your own delegates. Most of the time you should just use EventHandler, Func, Predicate, or Action.

What is difference between delegate and interface in C#?

Delegates and Interfaces are two distinct concepts in C#. Interfaces allow to extend some object's functionality, it's a contract between the interface and the object that implements it, while delegates are just safe callbacks, they are a sort of function pointers.


1 Answers

You can use any of these:

public delegate double CustomerDelegate(int test); public interface ITest {     EventHandler<EventArgs> MyHandler{get;set;}     CustomerDelegate HandlerWithCustomDelegate { get; set; }     event EventHandler<EventArgs> MyEvent; } 
like image 75
eglasius Avatar answered Sep 23 '22 23:09

eglasius