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

2019-03-09 17:41发布

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

7条回答
淡お忘
2楼-- · 2019-03-09 18:21

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?

查看更多
我命由我不由天
3楼-- · 2019-03-09 18: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();
        }
    }
}
查看更多
虎瘦雄心在
4楼-- · 2019-03-09 18: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;
}
查看更多
Root(大扎)
5楼-- · 2019-03-09 18:37

As others have mentioned, you can only define delegates outside of the interface.

There is little to nothing wrong w/ using delegates. Personally I think that Func<int, double> is less desirable than using delegates:

  1. You cannot name the arguments, so the argument meaning can be ambiguous
  2. It is old news that Events are not thread safe, thus the following code is not ideal:

    if (MyFuncEvent != null)
    {
        MyFuncEvent(42, 42.42);
    }
    

    See: http://kristofverbiest.blogspot.com/2006/08/better-way-to-raise-events.html

    The safer code is:

    MyFuncEventHandler handler = MyFuncEvent;
    if (handler != null)
    {
        handler(42, 42.42);
    }
    
  3. You have to duplicate the signature of the event if you want to save it to a variable (or you could use var, which I dislike). If you have lots of arguments then this could get very tedious (again, you could always be lazy and use var).

    Func<int, double, string, object, short, string, object> handler = MyFuncEvent;
    if (handler != null)
    {
        handler(42, 42.42, ...);
    }
    

Delegates save you from having to duplicate the signature of the method/event every time you want to assign it to a variable type.

查看更多
仙女界的扛把子
6楼-- · 2019-03-09 18:40

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.

查看更多
【Aperson】
7楼-- · 2019-03-09 18:42

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)

查看更多
登录 后发表回答