blogs

miércoles, 17 de junio de 2015

Extension Methods

Extension Methods
What make when we want  extend a existing type without modifier its definition.
  • We must create a static method in a static class.
  • We must use the first parameter to point the type that we can extend.
  • We must precede the first parameter with the this keyword .

For example:

public static bool ContainsNumbers(this string s)
{

}

Invoke extension methods
The method must be invoke like any other method.
For example:

string text= "Text with mumbers2";
if(text.ContainsNumbers)
{

}

In this case we will use the transport project.

We create two methods, one of them doesn't receive attribute and the other receive the attributes. We will use the class "Car" to make the extension methods.

Create a new class and a unit test

Run the first method



Run the test method





 Result  of the executing the first method.


Run the second method







Result of the executing the second method.


No hay comentarios:

Publicar un comentario