Flashcards in Extension Methods Deck (5)
What is an extension method?
Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type.
How to declare an extension method?
Via a static class with a static method. The method must has a this (only one, even for multiple params) keyword in the beginning.
static public double ToDouble(this string data)
What is a good place to store extension methods?
In its own namespace... e.g: Model.Employee.Extensions, or Your.Current.Namespace.Extensions
Can an extension be called in a regular way (MyStaticClass.DoSomething(param))?
Yes... the static class reamains as is. Actually, the "this" keyword makes the static class an extension.