Chris Ward
Chris Ward

Reputation: 718

Distinguishing Between Overloaded Generic and Non-Generic Methods

This must be covered somewhere but I'm having difficulty expressing my search criteria, so...

Below are three instance methods declared by the same type.

void Invoke(int timeout);
void Invoke<T>(T data);
T Invoke<T>(int timeout);

I want to invoke the second method, passing an Int32 argument and without using reflection. Options include:

Invoke(1);
Invoke<int>(1);
Invoke((int)1);
Invoke<int>((int)1);

However, none of these calls the desired method. Can I achieve what I want or should I resort to method-renaming?

Note that if the third method did not exist, I could simply do this:

Invoke(1); // Invokes the first method
Invoke<int>(1); // Invokes the second method

Upvotes: 2

Views: 81

Answers (1)

tukaef
tukaef

Reputation: 9214

Using named arguments:

Invoke(data: 1);

Upvotes: 4

Related Questions