I'd like to do something like the following, but because T is essentially just a System.Object this won't work. I know T can be constrained by an interface, but that isn't an option.
public class Vborr<T> where T : struct
{
public Vborr()
{
public T Next()
{
if ( typeof( T ) == typeof( Double ) )
{
// do something for doubles
}
if ( typeof( T ) == typeof( Float ) )
{
// do something different for floats..
}
}
}
I frequently find C# generics lacking.
Thanks!
Paul
The whole point of generics is that you can do the same thing for any valid type.
If you're truly doing something specific for the types, then the method isn't generic anymore and should be overloaded for each specific type.
public class Vborr<T> where T : struct
{
public virtual T Next() { // Generic Implementation }
}
public class VborrInt : Vborr<int>
{
public override int Next() { // Specific to int }
}
public class VborrDouble : Vborr<double>
{
public override double Next() { // Specific to double }
}