Keith Adler
Keith Adler

Reputation: 21178

Overload Anonymous Functions

Still wrapping my head around Delegates and I'm curious: Is it possible to overload anonymous functions?

Such that:

delegate void Output(string x, int y);

Supports:

Output show = (x, y) => Console.WriteLine("{0}: {1}", x.ToString(), y.ToString());

And:

delegate void Output(string x, string y);

Allowing:

show( "ABC", "EFG" );

And:

show( "ABC", 123 );

Upvotes: 1

Views: 278

Answers (2)

AlwaysAProgrammer
AlwaysAProgrammer

Reputation: 2919

You can probably use Generic Delegates.

public delegate void Output<T1,T2>(T1 x, T2 y);

Upvotes: 3

kemiller2002
kemiller2002

Reputation: 115420

No you can't overload a delegate like that.

This is a type

delegate void Output(string x, int y);

changing it to this:

delegate void Output(string x, string y);

would redefine it.

It would be kinda like defining two different classes with the same name (in the same namespace).

Upvotes: 3

Related Questions