operator+=

G

Guest

Is there any way to define the above operator for my own class? I seem to be
able to define every single one, except the one I want to (always the way :)

The main reason I ask is that the event interfaces use the "Event += new
EventHandler(..)" syntax - I am not sure how this gets implemented. Is this
done via operator+?

I am using C# 2.0
 
F

Frans Bouma [C# MVP]

Fil said:
Is there any way to define the above operator for my own class? I
seem to be able to define every single one, except the one I want to
(always the way :)

The main reason I ask is that the event interfaces use the "Event +=
new EventHandler(..)" syntax - I am not sure how this gets
implemented. Is this done via operator+?

I am using C# 2.0

Yes it's the + operator. :)

FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
 
G

Guest

Yes, of course - but the operator+= gives you additional capabilities to
improve efficiency, by modifying the item inline, rather than creating a new
instance

Not sure if you're aware, but C++ has an operator+=; you dont need to hack
it with operator+.

Regards, Fil.
 
S

Stoitcho Goutsev \(100\)

Fil,

1. In c# you overload += by overloading the + operator.
2. += for events is handled by the C# compiler and doesn't involve operator
overloading. Some may argue with this statement, but what I mean is that
there is no 'op_*' method generated.
 
G

Guest

1. In c# you overload += by overloading the + operator.

Unfortunately, I cannot do this - since the type of operation I am doing
modifies the class in-place, rather than creating a new instance.

I could dodge this and implement an in-place addition, but that would
produce unexpected results where the user actually did a b + c, instead of b
+= c
2. += for events is handled by the C# compiler and doesn't involve operator
overloading. Some may argue with this statement, but what I mean is that
there is no 'op_*' method generated.

Right - thought that might be the case.

Regards, Fil.
 
M

Marc Gravell

Why the fascination with the += operator in this case? Abusing it is (as you
yourself say) likely to cause grief - so why not just have a .Combine() or
..Append() method (peronally I think .Add() might be confusing as a name, as
you might expect this to be a non-consequential function that returns the
result).

With intellisense this doesn't really add an effort, and it keeps things
clean and simple. Plus you can specify such a method on an interface, which
you can't do with an operator.

Marc
 
G

Guest

The main reason I ask is that the event interfaces use the "Event += new
EventHandler(..)" syntax - I am not sure how this gets implemented.

Fil, it sounds like you might be looking for the 'add' and 'remove' methods
for maintaining event lists. The syntax is similar to 'get' and 'set' with
properties. It's very poorly documented (the keywords don't even appear in
the C# keyword list)

You'll find examples in MSDN -- C# programming guide "How to: Implement
Interface Events"

- KH
 
G

Guest

Why the fascination with the += operator in this case? Abusing it is (as you
yourself say) likely to cause grief

+= is neat because that describes exactly what I want to do. Abusing
operator+ would be a bad thing, yes. But if I could get my hands on
operator+= I would definitely use it.

- so why not just have a .Combine() or
..Append() method (peronally I think .Add() might be confusing as a name, as
you might expect this to be a non-consequential function that returns the
result).

Yes, I will use .Add(other) - that seems to next most sensible/obvious
solution.

Thanks for the post!

Regards, Fil.
 
F

Frans Bouma [C# MVP]

Fil said:
Yes, of course - but the operator+= gives you additional capabilities
to improve efficiency, by modifying the item inline, rather than
creating a new instance

Not sure if you're aware, but C++ has an operator+=; you dont need to
hack it with operator+.

Unfortunately, C# doesn't have the same operator voodoo power as C++
has, so you can only override a fixed, limited set of operators.

FB
Regards, Fil.


--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
 
M

Marc Gravell

+= is neat because that describes exactly what I want to do.
IMO (for what that matters) I disagree; "x += y;" is generally considered as
shorthand for "x = x + y;". This means reassigning the value of x; indeed,
this is what happens (via the compiler) for events and delegates :
Delegate.Combine is a static method that returns a new Delegate, so {event}
+= {handler} translates to (via the backing delegates and event pass-thrus):
{delegate backer} = Delegate.Combine({delegate backer}, {handler});

What you want to do is leave the original variable (in terms of references,
not contents) the same. To me, this is not "+="; this is an instance
operator.

Anyways, that's just my take ;-p

Marc
 
S

Stoitcho Goutsev \(100\)

Fil,

Yes that is the difference in overloading these operators in c# and c++.
 

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

Top