cannot convert from 'T' to 'sbyte' using Generics

Posted on 2006-11-06
Last Modified: 2008-02-01
Here's my function:

static public void SetMin<T>(ref T a, T b) {  // Invalid - Cannot convert from'T' to sbyte
      a = Math.Min(a, b);

Visual Studio 2005 gives the errors:
The best overloaded method match for 'System.Math.Min(sbyte, sbyte)' has some invalid arguments
Argument '1': Cannot convert from 'T' to 'sbyte'
Argument '2': Cannot convert from 'T' to 'sbyte'

Why is this?  If I unwrap this method and declare directly with sbyte, the compiler is fine with it:

static public void SetMin(ref sbyte a, sbyte b) {  // Works fine
      a = Math.Min(a, b);
Question by:rschaaf
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 13

Accepted Solution

joechina earned 125 total points
ID: 17881295
Math.Min has many overloaded methods but all accepts number type parameters.
When compiling your generic function, the compiler doesn't know the type T. There is no way to compile this.

Try this:

static public T GetMin<T>(T a, T b) where T : IComparable
            if (a.CompareTo(b) <= 0)
                return a;
                return b;
LVL 48

Expert Comment

ID: 17881297
I don't know way to do this using Math function, but this one can be used as replacement:

        static public void SetMin<T>(ref T a, T b) where T :IComparable<T>
            a = a.CompareTo(b) < 0 ? a : b;
LVL 48

Expert Comment

ID: 17881304
Oops, sorry for duplicated post.
LVL 48

Expert Comment

ID: 17881315
Actually, it is not 100% duplicated, because I use IComparable<T> :)
LVL 13

Expert Comment

ID: 17881641
True, IComparable<T> is better.

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor ( Top Charts is a view in which you can set seve…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

687 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question