Template function in C# - Return Type? -


It looks like C # templates does not support C ++, for example

  template & lt; Class myType & gt; MyType GetMax (myType a, myType b) {Return (A & G; B: A: B); }  

I want my function to have a return type based on its parameters, how can I get it in C #? How to use templates in C #

Edit: Can I use the object and getType for the same purpose?

In C # the closest generic of C ++ templates - but they too Especially, you are not able to close between normal code values ​​in & gt; You can not use operators like because the compiler does not know about them (and you can not restrict the types depending on operators) On the other hand, you can type:

 < Code> PublicT GetMax & lt; T & gt; (TLHS, TCLS) {Return Balanar & lt; T & gt; Default.Compare (LHS, RAS) & gt; 0? Lhs: rhs; }  

or

  public T getmax & lt; T & gt; (TLHS, TCLS) where T: ICemperable & lt; T & gt; {Return lhs.CompareTo (rhs) & gt; 0? Lhs: rhs; }  

Note that the first of these is zero-secured; The second is not.

The full description of the generic stack is beyond the scope of the overflow north; Have some information, or consult your favorite C # book.


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -