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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How does visual studio diferentiate normal methods from extension methonds?

A

extension methods has a transparent box with down arrow.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly