Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restrict generic T to specific types

Tags:

c#

generics

I have the following method:

public static IResult<T, String> Validate<T>(this T value) {
} // Validate

How can I restrict T to be Int16, Int32, Int64, Double and String?

like image 631
Miguel Moura Avatar asked Jun 13 '15 14:06

Miguel Moura


People also ask

How do you restrict a generic class?

Whenever you want to restrict the type parameter to subtypes of a particular class you can use the bounded type parameter. If you just specify a type (class) as bounded parameter, only sub types of that particular class are accepted by the current generic class.

Where is generic type constraint?

The where clause in a generic definition specifies constraints on the types that are used as arguments for type parameters in a generic type, method, delegate, or local function. Constraints can specify interfaces, base classes, or require a generic type to be a reference, value, or unmanaged type.

Can generic classes be constrained?

You can constrain the generic type by interface, thereby allowing only classes that implement that interface or classes that inherit from classes that implement the interface as the type parameter. The code below constrains a class to an interface.

Which of the following generic constraints restricts the generic type parameter to an object of the class?

Value type constraint If we declare the generic class using the following code then we will get a compile-time error if we try to substitute a reference type for the type parameter.

How do I restrict a generic class to a specific class?

You can only use Lion or CuteLion type. We can restrict a generic type to only allow type parameters of a specific class, or classes that inherit from that specific base class. Following the example of the previous section, let’s create a generic class which is restricted to the Lion class.

What is a generic type constraint?

Maybe you even misunderstood something about generic constraints. They don't restrict type, and they are not related to runtime. They are constraints for the generic parameters of a generic type, not for generic types.

How to restrict the type parameter to subtypes of a class?

Whenever you want to restrict the type parameter to subtypes of a particular class you can use the bounded type parameter. If you just specify a type (class) as bounded parameter, only sub types of that particular class are accepted by the current generic class.

Is it possible to create a generic type with generic parameters?

You cannot really do this with generics. Maybe you even misunderstood something about generic constraints. They don't restrict type, and they are not related to runtime. They are constraints for the generic parameters of a generic type, not for generic types.


2 Answers

You can only do this:

public static IResult<T, String> Validate<T>(this T value) where T: int
{
    //validate
} 

Only classes and interfaces can be used as constraint.

like image 104
Sjoerd222888 Avatar answered Oct 07 '22 04:10

Sjoerd222888


You can't restrict generics in that way, you can only choose a single class as a constraint. You must either make 5 overloads or find a interface all 5 of those things share and use that. Which option you choose will depend on what Validate doe.

Here is how you would do the overloads.

public static IResult<Int16, String> Validate<T>(this Int16 value) {
} // Validate

public static IResult<Int32, String> Validate<T>(this Int32 value) {
} // Validate

public static IResult<Int64, String> Validate<T>(this Int64 value) {
} // Validate

public static IResult<double, String> Validate<T>(this double value) {
} // Validate

public static IResult<String, String> Validate<T>(this String value) {
} // Validate

Here is by using a common interface, all of the members you list Implement IConvertible so you could restrict by that, however this will allow any IConvertible not just the 5 you listed.

public static IResult<T, String> Validate<T>(this T value) where IConvertible {
} // Validate
like image 23
Scott Chamberlain Avatar answered Oct 07 '22 03:10

Scott Chamberlain