So output print out all the element of list B, rather than printing elements which are not present in list A. Problem here is when Except method applied on user-defined datatype it tries to compare references of object to find difference between lists.

Resolution
To resolve problem, there is need to use Except overload method which takes list as first input and Custom Equality Comparer as its second parameter for type. Custom Equality comparer helps to do comparison between object of list and returns difference between list.

Sunday, March 20, 2016

Post is explains basics of Events & Delegate and most importantly explains what is difference between both.
Both Event & Delegate is based on overserved pattern of GOF, which is based on giving notification of change in one thing (object) to all others who is interested in change. Below image is graphical interpretation of same.

Delegates
Delegate in C# is reference type, which hold reference to the function and invokes function when called with Invoke method. If one is coming from C++/C background, delegate is like pointer to function which points to function.
To understand delegate better way have look to below sample code.

Events
Event in C# is of type Delegate, which means that if one wants to use event than one must need to define delegate first. Event can have multiple event-handler functions which are of signature like delegate which get called when event raised by some event in application Simple Example is Button click on UI. Event is very useful to create notification.
To understand delegate better way have look to below sample code.

Event VS Delegate
Both event and delegate does the same task, which is hold event handlers and call them when delegate/event invoked. So one always has question, what is need of Event in language C# when one can achieve same thing with Delegate.

Answer is Event is wrapper on Delegate type.

Problem With Delegate
Let’s understand this, using same class defined above which is “DelegateTest”.

Its assigns new delegate reference to delegate Print and override value assigned in constructor

Its assigns “null” to Print delegate and removes all added function.

Problem with Delegate is that one can easily override delegate property and that lead to error or serious issue. That means one cannot use delegate as public property.

Solution with Event
To avoid above problem C# has Events, Which defines wrapper around delegate. (Below code make use of EventTest class defined above and as form above code PrintEvent is wrapper around Print delegate).

Event is very helpful to create Notification system. Same is not possible with delegate because delegate cannot be exposed as public.

Delegate is very helpful to create call back function i.e can pass delegate as function argument, which is not possible with Event.

Conclusion
Event and Delegate are both follows Observer pattern. Difference between both is Event wraps Delegate type and which makes delegate not modifiable in terms of changing reference i.e. assign new object is not possible.

Above code assigns reference of already created object cust to cust1. Because cust1 holding reference to same object as cust1 modification to cust1 also change cust. This means both cust and cust1 points only to one object.

Now most of developer thinks as cust is null than cust1 is also null. But this is not true because when you give new reference to cust , both cust and cust1 point to different location.

Assigning null cust variable is can also be as below

Customer cust = new Customer { ID =2 , Name =”customer abc”}

So when you assign new value to any reference its point to new memory location i.e. new object. Which also means that assigning new value to reference variable is doesn’t affect old reference variable.

What happens in case of method?

Now Consider scenario where developer pass reference variable to method

Above discussion it’s clear that modification to reference variable happens when you do change in original reference variable and assigning new object to variable does modify the reference i.e. memory location object points to.