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?
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.
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.
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.
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.
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.
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.
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.
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.
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.
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
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