Extension Methods

Extend the functionality of a type without changing its source code.

Add functionality

There may be situations when we need to extend a class or struct’s functionality. There are several approaches we could take.

Change the source code

We could go to the source code of the type and make whatever changes we need. Consider the following Printer class. Its purpose is to print the Message property in a variety of ways. If we need additional functionality, we can simply add another method or edit existing ones:

Create a free account to view this lesson.

By signing up, you agree to Educative's Terms of Service and Privacy Policy