Reputation: 533
I have an event and I associate my handler with it. When I write my handler method how do I know which arguments the function takes?
example:
// Add an event handler to be called whenever there is new color frame data
this.sensor.ColorFrameReady += this.SensorColorFrameReady;
this.sensor.AllFramesReady += this.AllFramesReady;
//handler
private void AllFramesReady(object sender, AllFramesReadyEventArgs allFramesReadyEventArgs)
{
throw new NotImplementedException();
}
How do I know that the arguments of my function are object sender
and all frames ready args?
Upvotes: 3
Views: 284
Reputation: 9270
A manual way to do it in Visual Studio is to type this.sensor.ColorFrameReady +=
and then press Tab twice to see what the default implementation is. Do this similarly for any other events/delegates for which you want to see the signature.
Doing it like this does not require that you have the source code to the event and delegate (which you may not have).
In my years of C# coding, I have never actually found a better way of doing this. (I haven't actually looked for a solution, but there isn't any obvious one.)
Upvotes: 4
Reputation: 6717
An event has a delegate type. The delegate type defines the signature of the handler method. So look up the event delegate type, and you will find the required signature.
Upvotes: 1
Reputation: 40403
In Visual Studio: right-click on the event name -> Go to Definition. This will take you to the event. Right-click on the handler class name -> Go to Definition. This will take you to the definition of the delegate, which gives you your method signature.
This may depend slightly on your Visual Studio settings.
Upvotes: 2
Reputation: 203827
You look up the documentation for that event. It will specify what delegate defines that event. You can then look up the documentation for that delegate to see what the signature of a function must be to match the delegate.
Or you could rely on Visual Studio to tell you instead of looking it up, which is what most people do. (Hovering over the event will tell you what the delegate must be, or typing SomeEvent +=
into your keyboard will prompt you with an option to create a new stub of an event handler of the proper signature.)
Note that the name of the arguments is irrelevant (use whatever you want), only the types matter.
Upvotes: 6