sharif rifat
sharif rifat

Reputation: 69

Peculiar Method method definition in C#

I am working with C#. I have never seen a method declaration like this

private AuthorizationContext GetAuthorizationContext<TController>() where TController : ControllerBase, new()
    {
    }

Can any one please explain me what does these means. I have clear idea about generic class. my portion of query is 'where TController : ControllerBase, new()'

Upvotes: 2

Views: 73

Answers (1)

Despertar
Despertar

Reputation: 22392

This is a type constraint. It specifies certain conditions the generic type (TController) must meet. In this case it must derive from ControllerBase and have a parameterless constructor.

http://msdn.microsoft.com/en-us/library/d5x73970.aspx

Sometimes it doesn't make sense that ANY type is used as a generic type parameter. For example if I want to ensure that type T has certain methods I can enforce that it implements a given interface. This provides additional type safety and compile-time checking.

Upvotes: 6

Related Questions