Extension
Extension add extra methods to an existing class.
There are some rules:
- Extension is a static class
- The method is static
- The first parameter of the method is
this T target
.
namespace MyExtension
{
public static class StringExtensions
{
public static string Shorten(this String str, int numOfWords)
{
if (numOfWords == 0)
return "";
var words = str.Split(" ");
if (words.Length <= numOfWords)
return str
return string.Join(" ", words.Take(numOfWords));
}
}
}
using MyExtension.StringExtensions;
var post = ".......";
var shortPost = post.Shorten(3);
Extension is rarly created by the developers. Devlopers usually use them. (e.g. Linq
)