This workshop will be retired on May 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
Extension methods make it easy to add functional methods to any type.
LINQ extension methods defined in System.Linq.Enumerable
Follow these guidelines when writing extension methods:
- Only create extension methods that you could realistically see as being part of the original class.
- Because methods can be applied to interfaces, we need to be very careful that weβre sticking to the interface, and not creating methods that only make sense for some implementations of the interface.
- Only write extension methods for reusable code. Assume the extension will be reused by others and code it accordingly.
- Extension methods should have a very specific and straight-forward purpose.
- Their implementation should be obvious from the way they are named. They should also be well documented.
- They should be in a limited namespace. This makes it more obvious to coders when theyβre using an extension method and it makes it easier to scope methods to files. Don't forget about "using static".
- Extension methods are highly reusable and you never know how theyβll be used. So they need to be tested thoroughly.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up