Function with uncertain return type

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • RP

    Function with uncertain return type

    I have a function to return minimum of n numbers. Code given below:

    =====[ CODE ]=============== =============== =
    private int FindMinimum(int[] numbers)
    {
    int temp, i;
    temp = 0;
    for (i=0; i<= numbers.length; i++)
    {
    temp = Math.Min(temp, numbers[i]);
    }
    return temp;
    }
    =============== =============== ============

    I have not checked the above code. I actually want that the return
    type be generic, so that I can use this function for int, double or
    decimal. How to modify the above function to use generic return type?
  • Marc Gravell

    #2
    Re: Function with uncertain return type

    It is worth mentioning that .NET 3.5 provides this functionality "out
    of the box" via Enumerable.Min< T>() etc. Also, Comparer<T>.Def ault can
    be used if you don't want to enforce the constraint in the API; while
    constraints are a good thing, the downside is that it tends to
    snowball, so higher up functions end up with 3 or 4 constraints that
    are only used in edge-cases. This approach [Comparer<T>.Def ault, no
    constraint] is what LINQ uses (.NET 3.5).

    Marc

    Comment

    Working...