views:

207

answers:

1

Is it possible to write an inline generic method? For example, how can I translate the below method into an inline delegate.

public TUser Current<TUser>() where TUser : User
{
     return getCurrentUser() as TUser;
}

Even just being able to call

Func<User> userFunc = new Func<User>(Current<User>);

would be useful.

+3  A: 

You can use a lambda expression in C# 3.0:

Func<User> userFunc = () => getCurrentUser() as User;

or

Func<User> userFunc = Current<User>;
LBushkin
Since the method Current<User> already have a matching type you should be able to simply write 'Func<User> userFunc = Current<User>;'
Johan Kullbom