There is an AuthenticationBase
class in WCF RIA Services. The class definition is as follows:
// assume using System.ServiceModel.DomainServices.Server.ApplicationServices
public abstract class AuthenticationBase<T>
: DomainService, IAuthentication<T>
where T : IUser, new()
What does new()
mean in this code?
where T : new() Means that the type T must have a parameter-less constructor. Having this constraint will allow you to do something like T field = new T(); in your code which you wouldn't be able to do otherwise.
The new operator creates a new instance of a type. You can also use the new keyword as a member declaration modifier or a generic type constraint.
new is a Java keyword. It creates a Java object and allocates memory for it on the heap. new is also used for array creation, as arrays are also objects.
: having recently come into existence : recent, modern. I saw their new baby for the first time. a(1) : having been seen, used, or known for a short time : novel.
It's the new constraint.
It specifies that T
must not be abstract and must expose a public parameterless constructor in order to be used as a generic type argument for the AuthenticationBase<T>
class.
Using the new() keyword requires a default constructor to be defined for said class. Without the keyword, trying to class new() will not compile.
For instance, the following snippet will not compile. The function will try to return a new instance of the parameter.
public T Foo <T> ()
// Compile error without the next line
// where T: new()
{
T newInstance = new T();
return newInstance;
}
This is a generic type constraint. See this MSDN article.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With