jay's old blog

this blog will be deleted soon - please visit my new blog - https://thesanguinetechtrainer.com

Extending Types – Overriding and Extension methods

The entire basis for an object oriented language such as c sharp is that you can extend. Two quick ways you can do this are via overriding (methods) and extension (methods). 

When you use extension methods, you are essentially adding methods outside the actual class definition, but still making it available to all instances of the object. I cannot think of too many scenarios where you may do this. Mostly because, if I want a class to have an extra method, I could simply add it to the original class definition or I could have a derived class that contains the base class stuff anyway. 

However, let's says I wish to make a quick addition to a class definition, without having to update the original class definition, I could use the extension method to do the same.  

As always, find attached code at our code repository to see how it works. 

Overriding methods is pretty straightforward. You have already read about how methods can be overloaded. In this case, you simply derive a class and then override the methods you wish to override. Of course, there may times when you don’t want a method or the class (which contains said method) to be overriden. If so, you can 'seal' a method or the entire class with the 'sealed' keyword. 

As always, code available in our repository

Follow me on twitter, facebook and instagram for more updates. Thanks!

Comments are closed