Extension Methods Flashcards Preview

.NET / C# > Extension Methods > Flashcards

Flashcards in Extension Methods Deck (5)
Loading flashcards...

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.


How does visual studio diferentiate normal methods from extension methonds?

extension methods has a transparent box with down arrow.