Question from today’s webcast – What’s the difference between Partial Methods & Virtual Methods?


In today's webcast on LINQ to SQL we had a lot of questions. One had to do with understanding Partial Methods. The main difference between a virtual method (think OO) and partial methods (in 3.5) is that virtual methods can be implemented by various descendents differently based on what type of object you're working with. Think of an Animal object having an Eat method. This would be implemented differently for a Fish object than a Horse.


Unlike virtual methods you partial methods can be implemented only once. They are a sort of hook that you can choose to use and if not they are optimized away from the compiled code. This allows you to extend the functionality of a class with your own behavior. In LINQ to SQL the object mapping layer provides a collection of events that are implemented as partial methods. In this way we were able to run our custom logic when the company name changed.


Great question!


Mike

Skip to main content