Generic Methods

  • Thread starter Thread starter Jasper Kent
  • Start date Start date
J

Jasper Kent

I've just been playing around with the Visual Studio 2005 preview, and
obviously one of the first things I did was try to write a Max template.

I can write:

public static T Max<T> (T l,T r) where T :
System.Collections.Generic.IComparable <T>
{
return l.CompareTo (r) > 0 ? l : r;
}

But I can't find a way to do the comparison with operator < (because
operators are static and therefore cannot be defined in interfaces). Is it
possible to reference operators (or any static methods) on template
parameter objects?

Also, are there things like Max and Min already defined as templates in the
..NET library?

As you may have guessed, I come from a C++ background.

Thanks,

Jasper Kent.
 
Jasper,
But I can't find a way to do the comparison with operator < (because
operators are static and therefore cannot be defined in interfaces). Is it
possible to reference operators (or any static methods) on template
parameter objects?

No, operator constraints aren't supported unfortunately.

Also, are there things like Max and Min already defined as templates in the
.NET library?

I don't think they are, at least I haven't seen anything like that
yet.



Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top