Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a generic C# method that can return either double or decimal?

I have a method like this:

private static double ComputePercentage(ushort level, ushort capacity)
{
      double percentage;
      if(capacity == 1)
        percentage = 1;

      // do calculations...
      return percentage;
}

Is it possible to make it of a generic type like "type T" where it can return either decimal or double, depending on the type of method expected (or the type put into the function?)

I tried something like this and I couldn't get it to work, because I cannot assign a number like "1" to a generic type. I also tried using the "where T :" after ushort capacity) but I still couldn't figure it out.

private static T ComputePercentage<T>(ushort level, ushort capacity)
{
      T percentage;
      if(capacity == 1)
        percentage = 1; // error here

      // do calculations...
      return percentage;
}

Is this even possible? I wasn't sure, but I thought this post might suggest that what I'm trying to do is just plain impossible.


EDIT

Thanks to all who responded, many good answers. As Tomas pointed out, this is probably best done in two separate methods. As pointed out by both TreDubZedd and TcKs, the best way to get the functionality I would want is to use an implicit conversion that could return either a double or decimal implicitly.

like image 715
CrimsonX Avatar asked May 27 '10 22:05

CrimsonX


People also ask

Can you do generics in C?

Unlike C++ and Java, C doesn't support generics. How to create a linked list in C that can be used for any data type? In C, we can use a void pointer and a function pointer to implement the same functionality. The great thing about void pointer is it can be used to point to any data type.

How do you create a generic class?

A Generic Version of the Box Class To update the Box class to use generics, you create a generic type declaration by changing the code "public class Box" to "public class Box<T>". This introduces the type variable, T, that can be used anywhere inside the class.

What are generic functions in C?

A generic function is a function that is declared with type parameters. When called, actual types are used instead of the type parameters.


2 Answers

In fact, you don't need generics but overloading. However you need overloading by return value's type which is supported by IL but is not supported by C#.

I prefere two methods for every return's value type:

static double  ComputePercentageDouble  (ushort level, ushort capacity)
static decimal ComputePercentageDecimal (ushort level, ushort capacity)

The alternative can be custome type with implicit cast operators:

decimal decimalPercentage = ComputePercentage( 1, 2 );
double  doublePercentage  = ComputePercentage( 1, 2 );

static DoubleDecimal ComputePercentage( ushort level, ushort capacity ) {
    DoubleDecimal percentage = default( DoubleDecimal );
    if ( capacity == 1 )
        percentage.Number = 1; // error here

    // do calculations...
    return percentage;
}


public struct DoubleDecimal {
    public decimal Number;

    public static implicit operator decimal( DoubleDecimal value ) {
        return value.Number;
    }
    public static implicit operator double( DoubleDecimal value ) {
        return (double)value.Number;
    }
}
like image 101
TcKs Avatar answered Sep 20 '22 13:09

TcKs


You might be able to use implicit conversion: http://msdn.microsoft.com/en-us/library/zk2z37d3.aspx

like image 29
TreDubZedd Avatar answered Sep 20 '22 13:09

TreDubZedd