What does where T : somevalue mean? I just saw some code that said where T : Attribute. I think this has something to do with generics but I am not sure what this means or what it is doing.
Does anyone know?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It is a constraint on a type parameter, meaning that the type
Tgiven to a generic class or method must inherit from the classAttributeFor example:
This is useful, because it allows the generic class to do things with objects of type
Twith the knowledge that anything that is aTmust also be anAttribute.In the example above, it’s okay for
GetTypeIdto query theTypeIdofattrbecauseTypeIdis a property of anAttribute, and becauseattris aTit must be a type that inherits fromAttribute.Constraints can also be used on generic methods, with the same effect:
There are other constraints you can place on a type; from MSDN: