Click here to Skip to main content
15,906,329 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,

Can anyone please tell me ,if "+=" operator is overloaded by Microsoft.
for eg:
delegate double demodel(int a)
demodel dd=classname.getsumdoublevale
demodel+= classname.getmuldoublevalue ------what exactly happens here?



regards
Chaithanya M
Posted
Comments
Pong D. Panda 4-May-11 9:11am    
Dont you have VS installed? You can run that code and find it yourself

 
Share this answer
 
Comments
Tarun.K.S 4-May-11 9:15am    
Excellent link. 5+
Kim Togo 4-May-11 9:18am    
Thanks Tarun.
Sergey Alexandrovich Kryukov 12-May-11 16:23pm    
Kim, I voted 5 for good reference.

I probably need to write my own article on the topic. This "+=" is not so trivial as many think. Did you know that the instance in immutable which is observed as the loss of referential integrity? Kinda important feature.

Please see my answer.
--SA
Kim Togo 13-May-11 4:27am    
Thanks SA. The CP article has helped me a lot to understand "delegate" and "event".
The question is not so trivial. If you look at the delegate instance dd you will find that this is an instance of some… class. This class actually encapsulate a lot of functionality. In particular, it has the container of handlers called "invocation list", accessible through dd.GetInvocationList(). This list is use to call all handlers in sequence as a result of invocation and pass the them their own instances of "self" (passed when a handler is added) and calling parameters passed from the invocation call.

The handler is added to the invocation list by "+" (for the first time) and "+=". Is it really so? Not exactly! The delegate instance looses its referential integrity on each "+="!

C#
static void ReportDelegateEquivalence(Delegate left, Delegate right) {
    FormatEqual formatEqual = delegate(bool value) { if (value) return string.Empty; else return "NOT"; };
    System.Console.WriteLine(
        "Delegates are {0} equal, they are referentually {1} equal",
        formatEqual(left == right),
        formatEqual(object.ReferenceEquals(left, right)));
} //ReportDelegateEquivalence

static void TestDelegateEquivalence() {
    TestDelegate delegate1 = delegate(string name) { return 1; };
    TestDelegate delegate2 = delegate(string name) { return 1; };
    //output: "Delegates are NOT equal, they are referentually NOT equal":
    ReportDelegateEquivalence(delegate1, delegate2);
    
    delegate1 = delegate2;
    //output: "Delegates are equal, they are referentually equal":
    ReportDelegateEquivalence(delegate1, delegate2);
  
    delegate2 += delegate(string name) { return 2; };
    //output: "Delegates are equal, they are referentually equal":     
    ReportDelegateEquivalence(delegate1, delegate2);
} //TestDelegateEquivalence


This test shows that the delegate instance does not actually add a new handler. Instead, a brand new delegate instance is created with extended invocation list. So, the delegate instance is immutable. The purpose is the behavior is improving multithreading support: what is one thread is through the call of invocation while another thread is performing "+="?

Credit to Nishant Sivakumar how explained the purpose of this feature to me.

[EDIT]
Please see my recent article on the topic: Dynamic Method Dispatcher[^]. In particular, see this chapter: Dynamic Method Dispatcher[^].

—SA
 
Share this answer
 
v2
Comments
parmar_punit 30-May-11 3:03am    
nice one, my 5
Sergey Alexandrovich Kryukov 30-May-11 3:14am    
Thank you, Parmar. Did you see the new article I recently referenced (after [EDIT])?
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900