Extension methods. Practices.
Extension methods are a language feature that allows static methods to be called using instance method call syntax. These methods must take at least one parameter, which represents the instance the method is to operate on.
The class that defines such extension methods is referred to as the "sponsor" class, and it must be declared as static. To use extension methods, one must import the namespace defining the sponsor class.
X AVOID frivolously defining extension methods, especially on types you don’t own.
✓ CONSIDER using extension methods in any of the following scenarios:
X DO NOT put extension methods in the same namespace as the extended type unless it is for adding methods to interfaces or for dependency management.
X AVOID defining two or more extension methods with the same signature, even if they reside in different namespaces.
✓ CONSIDER defining extension methods in the same namespace as the extended type if the type is an interface and if the extension methods are meant to be used in most or all cases.
X DO NOT define extension methods implementing a feature in namespaces normally associated with other features. Instead, define them in the namespace associated with the feature they belong to.
X AVOID generic naming of namespaces dedicated to extension methods (e.g., "Extensions"). Use a descriptive name (e.g., "Routing") instead.
Last updated