W
Wiktor Zychla
I've read several documents about upcoming C# generics and I still cannot
understand one thing.
Would it be valid to write a code like this:
class SomeClass
{
public void AMethod<T>(T a, T b)
{
T c = a + b; // ?
...
}
}
since + is defined for some simple types and can be overloaded for reference
types, then what kind of constraint should be used to allow the + in above
context?
or would it be forbidden to use standard binary operators in generic code?
(in fact, it would require to have the interface, IAddable, make all numeric
types implement the interface and then add "where T : IAddable" to the above
code)
if it is forbidden to use + in above context, then how will I create a
generic class that acts like this one above? (suppose I need to call the
method AMethod with any class that supports + operation, including <int>
<double> and other numeric types).
I would really like to hear from an expert on this issue.
Thanks in advance.
Wiktor Zychla
understand one thing.
Would it be valid to write a code like this:
class SomeClass
{
public void AMethod<T>(T a, T b)
{
T c = a + b; // ?
...
}
}
since + is defined for some simple types and can be overloaded for reference
types, then what kind of constraint should be used to allow the + in above
context?
or would it be forbidden to use standard binary operators in generic code?
(in fact, it would require to have the interface, IAddable, make all numeric
types implement the interface and then add "where T : IAddable" to the above
code)
if it is forbidden to use + in above context, then how will I create a
generic class that acts like this one above? (suppose I need to call the
method AMethod with any class that supports + operation, including <int>
<double> and other numeric types).
I would really like to hear from an expert on this issue.
Thanks in advance.
Wiktor Zychla