Extension Methods Flashcards
1
Q
What is an extension method?
A
Extension methods enable you to “add” methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type.
2
Q
How to declare an extension method?
A
Via a static class with a static method. The method must has a this (only one, even for multiple params) keyword in the beginning. e.g: static public double ToDouble(this string data)
3
Q
What is a good place to store extension methods?
A
In its own namespace… e.g: Model.Employee.Extensions, or Your.Current.Namespace.Extensions
4
Q
Can an extension be called in a regular way (MyStaticClass.DoSomething(param))?
A
Yes… the static class reamains as is. Actually, the “this” keyword makes the static class an extension.
5
Q
How does visual studio diferentiate normal methods from extension methonds?
A
extension methods has a transparent box with down arrow.