What does new() mean?

后端 未结 3 757

There is an AuthenticationBase class in WCF RIA Services. The class definition is as follows:

// assume using System.ServiceModel.DomainServices         


        
3条回答
  •  自闭症患者
    2020-12-01 21:51

    It means that a type used to fill the generic parameter T must have a public and parameterless constructor. If the type does not implement such a constructor, this will result in a compile-time error.

    If the new() generic constraint is applied, as in this example, that allows the class or method (the AuthenticationBase class in this case) to call new T(); to construct a new instance of the specified type. There is no other way, short of reflection (this includes using System.Activator, to construct a new object of a generic type.

提交回复
热议问题