Extension methods in C#
July 24, 2017 1 Comment
Introduction
Extension methods in C# allow you to extend the functionality of types that you didn’t write and don’t have direct access to. They look like integral parts of any built-in classes in .NET, e.g.:
DateTime.Now.ToMyCustomDate(); string.ToThreeLetterAbbreviation();
You can extend the following types in C#:
- Classes
- Structs
- Interfaces
You can extend public types of 3rd party libraries. You can also extend generic types, such as List of T and IEnumerable of T. You cannot extend sealed classes.