views:

339

answers:

6

It seems that c# does not support c++ like templates. For example

template <class myType>
myType GetMax (myType a, myType b) {
 return (a>b?a:b);
}

I want my function to have return type based on its parameters, how can i achieve this in c#? How to use templates in C#

EDIT: Can i use object and getType for the almost same purpose?

+1  A: 

You use Generics in C# to get template like functionality.

http://msdn.microsoft.com/en-us/library/ms379564(VS.80).aspx

public class MyType<T>
{
}
Foovanadil
A: 

Generics!

class MyClass<T> where T : IEnumerable (any parent class here!)
{
   T myGeneric {get; set;}
}
Gnostus
A: 

You'll want to have a look at generics. Syntax:

public T GetMax<T>(T a, T b) {
    return (a>b?a:b);
}

UPDATE because of some comments... I just wanted to provide a very quick overview. Yeah I guess it doesn't compile or execute properly. But this is the basic idea on how this would look like. You'll find more complete replies on this topic - meanwhile.

Execution Syntax:

int a = 5;
int b = 10;
int result = GetMax(a, b);

Hope this helps,

Regards, Thomas

moonground.de
I was about to say the same but that won't compile because of the line 'return (a>b?a:b);' He will need an interface constraint or something...
BioBuckyBall
You cannot compare a and b in this way, as the compiler has no idea what T is. What if the type T does not support comparison?
Matt Greer
+9  A: 

The closest to C++ templates in C# is generics - but they're not very close. In particular, you can't use operators like > between generic type values, because the compiler doesn't know about them (and you can't constrain types based on operators). On the other hand, you can write:

public T GetMax<T>(T lhs, T rhs)
{
    return Comparer<T>.Default.Compare(lhs, rhs) > 0 ? lhs : rhs;
}

or

public T GetMax<T>(T lhs, T rhs) where T : IComparable<T>
{
    return lhs.CompareTo(rhs) > 0 ? lhs : rhs;
}

Note that the first of these is null-safe; the second isn't.

A full description of generics is well beyond the scope of a Stack Overflow answer; MSDN has some information, or consult your favourite C# book.

Jon Skeet
I've heard C# in depth has some good explanations of generics :)
SWeko
The function i given was just a sample, What about functions other than comparison?
LifeH2O
@Life2HO: Well, what function do you want? Is it an instance method which is specified in an interface? If so, you're fine.
Jon Skeet
+1  A: 

Your code would become something like this:

public T GetMax<T>(T a, T b) where T : IComparable<T>
{
    return a.CompareTo(b) > 0 ? a : b;
}
Lee
+1  A: 

Generics in C# are not as powerful as templates in C++. What you want to do does not work in C#.

A hack/workaround for your situation is

public T GetMax<T>(T a, T b) where T: IComparable {
    if(a.CompareTo(b) > 0) {
        return a;
    }
    return b;
}
Matt Greer
The function i given was just a sample, What about functions other than comparison?
LifeH2O
@LifeH2O: It depends - is it specified in an interface? If so, you can constrain the generic type to implement the interface (or derive from a given base class, etc).
Jon Skeet
As a C++ developer you may find yourself disappointed with C#'s generics. But they are still useful in many scenarios, and worth learning.
Matt Greer