"Sam" <an*******@discussions.microsoft.com> a écrit dans le message de
news:38**********************************@microsof t.com...
I want to overload the + operator for ArrayList for adding elements:
public static int operator +(ArrayList l, Object o)
{
return (l.Add(o));
}
First I have discovered that the return value cannot be void and if I
change it to something like int, I have to always write like this: int i = list + 3; // I don't need i
The compiler doesn't accept just
list + 3;
is there a workaround? I just want to write
list + 3 ;
as a statement and not an expression.
My 2 cents: normally, an operator like +:
a) produces a result
b) does not modify its operands.
And you are trying to define a + operator that violates these 2 rules.
This is a bad idea. You should keep the method and only define operators
like + when you have a real algebra (operations that don't have side effects
on their operands). Otherwise, you will confuse the people who read your
code.
Bruno.