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

前端 未结 7 1018
清歌不尽
清歌不尽 2021-02-01 03:24

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

相关标签:
7条回答
  • 2021-02-01 04:15

    this is a delegate TYPE decalaration...

    public delegate returntype MyDelegateType (params)
    

    this cant be declared in an interface as it is a type declaration

    however using the type declaration above you CAN use a delegate instance

    MyDelegateType MyDelegateInstance ( get; set;)
    

    so delegate instances are OK but delegate type declarations aren't (in an interface)

    0 讨论(0)
  • 2021-02-01 04:20

    The documentation clearly says that you can define a delegate in an interface:

    An interface contains only the signatures of methods, delegates or events.

    MSDN: interface (C# Reference)

    However, in the remarks on the same page it says that an interface can contain signatures of methods, properties, indexers and events.

    If you try to put a delegate in an interface, the compiler says that "interfaces cannot declare types."

    The Ecma-334 standard (8.9 Interfaces) agrees with the remarks on that page and the compiler.

    0 讨论(0)
  • 2021-02-01 04:22

    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.

    May I ask what your delegate looks like?

    0 讨论(0)
  • 2021-02-01 04:22

    An interface method can accept a delegate as a parameter, no issues. (Maybe I'm not seeing the problem?) But if the intention is to specify an outbound call in the interface, use an event.

    There are so many little details, it's a lot easier to just show some code instead of trying to describe it all in prose. (Sorry, even the code sample is a bit bloated...)

    namespace DelegatesAndEvents
    {
        public class MyEventArgs : EventArgs
        {
            public string Message { get; set; }
            public MyEventArgs(string message) { Message = message; }
        }
    
        delegate void TwoWayCallback(string message);
        delegate void TwoWayEventHandler(object sender, MyEventArgs eventArgs);
    
        interface ITwoWay
        {
            void CallThis(TwoWayCallback callback);
    
            void Trigger(string message);
            event TwoWayEventHandler TwoWayEvent;
        }
    
        class Talkative : ITwoWay
        {
            public void CallThis(TwoWayCallback callback)
            {
                callback("Delegate invoked.");
            }
    
            public void Trigger(string message)
            {
                TwoWayEvent.Invoke(this, new MyEventArgs(message));
            }
    
            public event TwoWayEventHandler TwoWayEvent;
        }
    
        class Program
        {
            public static void MyCallback(string message)
            {
                Console.WriteLine(message);
            }
    
            public static void OnMyEvent(object sender, MyEventArgs eventArgs)
            {
                Console.WriteLine(eventArgs.Message);
            }
    
            static void Main(string[] args)
            {
                Talkative talkative = new Talkative();
    
                talkative.CallThis(MyCallback);
    
                talkative.TwoWayEvent += new TwoWayEventHandler(OnMyEvent);
                talkative.Trigger("Event fired with this message.");
    
                Console.ReadKey();
            }
        }
    }
    
    0 讨论(0)
  • 2021-02-01 04:26

    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;
    }
    
    0 讨论(0)
  • 2021-02-01 04:28

    A Delegate is a type which can't be declared in an interface. You might want to either use an event(if appropriate) or declare a delegate outside the interface but in the same namespace.

    This link may help- When to Use Delegates Instead of Interfaces

    0 讨论(0)
提交回复
热议问题