I was reading the book "Apress Pro LINQ: Language Integrated Query in C#" and I came across partial methods, but I really don't understand what is the need for them.
I think the examples in the book (properties before and after change) can be implemented using events. So any explanation?
Yes, you could achieve a similar effect with events as you can with partial methods. Partial methods are really just a way of letting code generators, primarily designers, to generate hooks for the non-generated code. Events could fill this role.
However there are advantages to partial methods over events in particular
They are not "needed", but desired for large scale applications. Extensive use of events leads to the Smart UI anti-pattern where the business logic is tightly coupled with the user interface, whereas partial functions allow you to better separate your concerns.
Here is a link to the MSDN C# programming guide on partial methods as well. http://msdn.microsoft.com/en-us/library/wa80x488.aspx
The compiler will remove calls to partial methods if there are no implementations. With the alternative of events, listeners would have to be checked at runtime (they would also need to be stored, etc). This allows partial methods to be more performant, especially when there are many of potential "events" and only a few have "listeners" registered.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With