Re: Cannot put delegate in interface - why not?

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Jon Skeet [C# MVP] wrote:
atwomey@xxxxxxxxx wrote:
When I try and place a delegate in an interface, like this :

public interface ITest {
double foo();
delegate void bar();
}

I get an error "delegates cannot declare types". What is it about a
delegate that makes it incompatible in an interface. I know that an
interface cannot have any implementation - is there some implementation
detail in a delegate that I don't know about?

You've tried to do something like:

public interface ITest
{
public class Foo
{
}
}

Declaring a delegate is declaring a type, which can't be part of an
interface. You should declare the delegate itself elsewhere - you can
have a member of the type which *uses* the delegate, but that's a
different matter.

Jon

Thanks for your replies - I think my question is : is the delgate a
type because it inherits from System.Delgates, even though the delegate
itself is not instantiated? I always thought of a delegate as a
"function pointer", but it's also a class and therefore a type.

.



Relevant Pages

  • Re: Cannot put delegate in interface - why not?
    ... public interface ITest { ... delegate void bar; ... I get an error "delegates cannot declare types". ... delegate that makes it incompatible in an interface. ...
    (microsoft.public.dotnet.languages.csharp)
  • Cannot put delegate in interface - why not?
    ... When I try and place a delegate in an interface, ... public interface ITest { ... delegate void bar; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: WPF Threading Model
    ... you don't have to declare the delegate ... you can declare your OnServerDataRecieved like so: ... myserver.DataReceivedHandler handler = ... arrives at the server, however I ran into some problems because of the WPF ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Event handler or Delegate?
    ... You need to declare your own event if you're going to write code to ... SelectedItemChanged event, because in the .NET framework Panels don't ... I also had to create a new type of delegate, ... override their behaviour, ...
    (microsoft.public.dotnet.framework.windowsforms.controls)
  • Re: how to wrap your brain around delegates?
    ... but you can simply declare a field of the ... delegate type to support that property. ... public class NotificationEventArgs: EventArgs ... private NotificationHandler notify; ...
    (borland.public.delphi.non-technical)