BreakHead
BreakHead

Reputation: 10672

Using Same Delegate Signature for Parameter and Parameterless Method

I have a method which takes method delegate having parameter

 public delegate void RunOperation(object o );
  public void Abort(RunOperation operationToRun, object obj)
  {
  }
  public void AllMessages()
  {
  }

Is it possible to pass AllMessage() as a delegate to Abort() ?

I don't want to create any new delgate for parameterless methods.

Thanks

Upvotes: 0

Views: 154

Answers (2)

Fischermaen
Fischermaen

Reputation: 12458

I would solve it in this way:

public void Abort(Action operationToRun, object obj)
{

}
public void Abort(Action<object> operationToRun, object obj)
{

}
public void AllMessages()
{

} 

Explanation:

Make an overload for the Abort method. First method takes an action without parameter and second takes an action with one parameter. So you can call Abort with a parameterless method and a method with an object parameter.

Upvotes: 0

Ray
Ray

Reputation: 46585

No and Yes.

You can like this:

Abort(_ => AllMessages(), null);

But you're just creating another method that calls AllMessages and doesn't use the object parameter.

Upvotes: 2

Related Questions