# C# Study - Extension Method
###### tags: `C#` `Study` `Extension Method`
# References:
* [Extension Methods (C# Programming Guide)](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods)
* [Extension Methods - C# 5.0 Pocket Reference [Book] - O'Reilly](https://www.oreilly.com/library/view/c-50-pocket/9781449335441/ch01s33.html)
---
# C# - [Extension Method](https://www.tutorialsteacher.com/csharp/csharp-extension-method)
Since C# 3.0
C# 也有 Extension!!
Extension methods, as the name suggests, are additional methods.
Extension methods allow you __to inject additional methods
without modifying, deriving or recompiling
the original class, struct or interface__.
Extension methods can be added to
__your own custom class__,
__.NET framework classes__, or
__third party classes or interfaces__.
In the following example,
``IsGreaterThan()`` is an extension method for ``int`` type,
which returns ``true`` if the value of the ``int`` variable
is greater than the supplied integer parameter.
```
int i = 10;
bool result = i.IsGreaterThan(100); //returns false
```
The ``IsGreaterThan()`` method is not a method of
``int`` data type (Int32 struct).
It is an extension method written by the programmer for the ``int`` data type.
The ``IsGreaterThan()`` extension method __will be available
throughout the application by including the namespace__
in which it has been defined.
``int`` 是 ``Int32`` struct
The extension methods have a special symbol
in intellisense of the visual studio, so that
you can easily __differentiate between class methods and extension methods__.
Now let's see how to write an extension method.
An extension method is actually a special kind of static method defined in a static class. To define an extension method, first of all, define a static class.
For example, we have created an IntExtensions class under the ExtensionMethods namespace in the following example. The IntExtensions class will contain all the extension methods applicable to int data type. (You may use any name for namespace and class.)
### Example: Create a Class for Extension Methods
```
namespace ExtensionMethods{
public static class IntExtensions {
}
}
```
Now, __define a ``static`` method as an extension method__
where __the first parameter__ of the extension method
specifies __the type on which the extension method is applicable__.
We are going to __use this extension method on ``int`` type__.
So __the first parameter must be ``int``__ preceded with the this modifier.
For example, the ``IsGreaterThan()`` method operates on ``int``,
so the first parameter would be, this int i.
### Example: Define an Extension Method
```
namespace ExtensionMethods {
public static class IntExtensions {
public static bool IsGreaterThan(this int i, int value) {
return i > value;
}
}
}
```
Now, you can include the ``ExtensionMethods`` namespace
wherever you want to use this extension method.
```
using ExtensionMethods;
class Program {
static void Main(string[] args) {
int i = 10;
bool result = i.IsGreaterThan(100);
Console.WriteLine(result);
}
}
```
要有一個 ``static class``!
method 要加 ``static`` 關鍵字,然後 第一個 parameter
一定要是 ``this type parameter_name``
Note:
The only difference between a regular ``static`` method and
an extension method is that __the first parameter of the extension method
specifies the type that it is going to operator on, preceded by the ``this`` keyword__.