A sample of writing extension methods:
And some tests for the extensions:
The couple things to note are:
- The class needs to be static.
- Use this keyword to specify what you’re extending.
- In the project you’re using this in, add a reference, and a using for your namespace.
- Naming schema is typically the type being extended plus “extensions”. I.e. String would be extended in StringExtensions class, DateTime in DateTimeExtensions, etc.
For more detail, watch Elton Stoneman’s PluralSight course on extension methods. Part one gives all the details on how to build them. Part two and three go into more depth on how to use them in difficult scenarios, and how to use reflection in order to use the methods when you wouldn’t otherwise be able to.