a constructor as a delegate - is it possible in C#?

Posted by akavel on Stack Overflow See other posts from Stack Overflow or by akavel
Published on 2009-10-21T13:04:51Z Indexed on 2010/03/22 10:01 UTC
Read the original article Hit count: 221

Filed under:
|
|

I have a class like below:

class Foo
{
  public Foo(int x) { ... }
}

and I need to pass to a certain method a delegate like this:

delegate Foo FooGenerator(int x);

Is it possible to pass the constructor directly as a FooGenerator value, without having to type:

delegate(int x) { return new Foo(x); }

?

EDIT: For my personal use, the question refers to .NET 2.0, but hints/responses for 3.0+ are welcome as well.

© Stack Overflow or respective owner

Related posts about c#

Related posts about constructors