Cannot put delegate in interface - why not?

A

atwomey

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?
 
M

Marc Gravell

"delegate void bar();"

is actually a type declaration of the "bar" type, and *not* an instance
member. Perhaps you mean:

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

Marc
 
J

Jon Skeet [C# MVP]

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
 
A

atwomey

Jon said:
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.
 
M

Marc Gravell

An ***instance*** of a delegate is (essentially) a function pointer;
however, starting with the word "delegate" (with a small d) here defines the
***type*** - which is primarily the method signature for the Invoke method -
i.e. in this case it takes nothing and returns nothing. If it was a large D
(Delegate) then it would be any instance. Confusing, eh?

I've amended my previous code (to compile; whoops), and added some examples:

public delegate void MyDelegate();
public interface ITest {
double foo();
MyDelegate bar {get; set;}
Delegate fred {get; set;}
}
//...
ITest somet = new TestClasss(); // that implements ITest
somet.bar = new MyDelegate(SomeMethod); // creates an instance of MyDelegate
with SomeMethod as the target
somet.fred = // pretty much any random delegate, but must invoke via
DynamicInvoke
//...
static void SomeMethod() {}

Note: to add confusion, in the RHS of 2.0 expressions, the word "delegate"
can signify an /anonymous/ delegate, which /is/ an instance, not a type -
e.g.

button.Click += delegate {Debug.WriteLine("Hi");};

Marc
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Thanks for your replies - I think my question is : is the delgate a
type because it inherits from System.Delgates,

Yes, of course.
even though the delegate
itself is not instantiated?

The example of Jon does not instantiate nothing, just declare a type inside
an interface, which is the error you are getting
I always thought of a delegate as a
"function pointer", but it's also a class and therefore a type.

In .NET all definitions are types. from enum to attributes to classes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top