T
Tom Jastrzebski
Hello everybody,
Here is the problem I came across experimenting with Generics.
I would like to write a class or a struct adding integer to any other,
initially undefined *numeric type*.
So, my struct would look more or less like:
struct MySum<T> where T : struct {
public static T AddInteger(T value, int i) {
return value + i;
}
}
Of course the above code does not compile since the compiler does not even
know whether <T> implements "+" operator or not.
Even it it would it would not know if <T> can be converted to Int32 or not.
I tried different solutions, but none of them worked.
The described problem is just a simplified illustration of an issue I
experienced trying to implemented more complex functionality.
Disclaimer: yes, I know that this is not the way generics are intended to be
used in the first place, but may be the solution exists.
Thanks,
Tomasz
Here is the problem I came across experimenting with Generics.
I would like to write a class or a struct adding integer to any other,
initially undefined *numeric type*.
So, my struct would look more or less like:
struct MySum<T> where T : struct {
public static T AddInteger(T value, int i) {
return value + i;
}
}
Of course the above code does not compile since the compiler does not even
know whether <T> implements "+" operator or not.
Even it it would it would not know if <T> can be converted to Int32 or not.
I tried different solutions, but none of them worked.
The described problem is just a simplified illustration of an issue I
experienced trying to implemented more complex functionality.
Disclaimer: yes, I know that this is not the way generics are intended to be
used in the first place, but may be the solution exists.
Thanks,
Tomasz