2

Ran into this while converting a VB.NET interface to C#; the VB version defines an event which doesn't conform to the typical (object sender, EventArgs e) signature:

VB

Public Class SomeType
    ' Does *NOT* inherit from EventArgs
End Class

Public Interface ISomething
    Public Event SomeEvent(sender as Object, value as SomeType)
End Interface

What's the C# equivalent for ISomething? My attempts so far have failed to compile:

1 Answer 1

9

You'll need a delegate type declaration:

public delegate void SomeEventHandler(object sender, SomeType value)

public class SomeType
{
}

public interface ISomething
{
    public event SomeEventHandler SomeEvent;
}

Or in .NET 3.5 you could use the built-in Action type:

public class SomeType
{
}

public interface ISomething
{
    public event Action<object, SomeType> SomeEvent;
}
Sign up to request clarification or add additional context in comments.

2 Comments

That's close to what I tried; to keep the delegate's name the same I tried declaring it within the C# interface--but it failed to compile. Is it possible to declare a delegate as part of an interface, or will the delegate have to reside outside of the interface?
It'll have to be outside the interface - unlike VB, C# doesn't allow nested types within an interface (which is a pain for a few things, but never mind).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.