[ACCEPTED]-Why can't I put a delegate in an interface?-interface
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
A Delegate is just another type, so you 6 don't gain anything by putting it inside 5 the interface.
You shouldn't need to create 4 your own delegates. Most of the time you 3 should just use EventHandler, Func, Predicate, or 2 Action.
May I ask what your delegate looks 1 like?
this is a delegate TYPE decalaration...
public delegate returntype MyDelegateType (params)
this 6 cant be declared in an interface as it is 5 a type declaration
however using the type 4 declaration above you CAN use a delegate 3 instance
MyDelegateType MyDelegateInstance ( get; set;)
so delegate instances are OK but 2 delegate type declarations aren't (in an 1 interface)
A Delegate is a type which can't be declared in an interface. You 4 might want to either use an event(if appropriate) or 3 declare a delegate outside the interface 2 but in the same namespace.
This link may 1 help- When to Use Delegates Instead of Interfaces
The documentation clearly says that you 10 can define a delegate in an interface:
An 9 interface contains only the signatures 8 of methods, delegates or events.
MSDN: interface (C# Reference)
However, in 7 the remarks on the same page it says that 6 an interface can contain signatures of methods, properties, indexers 5 and events.
If you try to put a delegate 4 in an interface, the compiler says that 3 "interfaces cannot declare types."
The 2 Ecma-334 standard (8.9 Interfaces) agrees 1 with the remarks on that page and the compiler.
As others have mentioned, you can only define 15 delegates outside of the interface.
There is little 14 to nothing wrong w/ using delegates.
Personally 13 I think that Func<int, double>
is less desirable than using 12 delegates:
- You cannot name the arguments, so the argument meaning can be ambiguous
It is old news that Events are 11 not thread safe, thus the following code 10 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); }
You 9 have to duplicate the signature of the event 8 if you want to save it to a variable (or 7 you could use
var
, which I dislike). If you 6 have lots of arguments then this could get 5 very tedious (again, you could always be 4 lazy and usevar
).Func<int, double, string, object, short, string, object> handler = MyFuncEvent; if (handler != null) { handler(42, 42.42, ...); }
Delegates save you from having 3 to duplicate the signature of the method/event 2 every time you want to assign it to a variable 1 type.
An interface method can accept a delegate 7 as a parameter, no issues. (Maybe I'm not 6 seeing the problem?) But if the intention 5 is to specify an outbound call in the interface, use 4 an event.
There are so many little details, it's 3 a lot easier to just show some code instead 2 of trying to describe it all in prose. (Sorry, even 1 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();
}
}
}
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.