Extension Methods
- This is a feature of C# 3.0.
Extension methods are a form of syntactic sugar providing the illusion of adding new methods to the existing class outside its definition. In practice, an extension method is a static method that is callable as if it were an instance method; the receiver of the call is bound to the first parameter of the method, decorated with keyword this:
public static class StringExtensions { public static string Left(this string s, int n) { return s.Substring(0, n); } } string s = "foo"; s.Left(3); // same as StringExtensions.Left(s, 3);See also
- Decorator pattern
Read more about this topic: C Sharp Syntax
Famous quotes containing the words extension and/or methods:
“Predatory capitalism created a complex industrial system and an advanced technology; it permitted a considerable extension of democratic practice and fostered certain liberal values, but within limits that are now being pressed and must be overcome. It is not a fit system for the mid- twentieth century.”
—Noam Chomsky (b. 1928)
“All men are equally proud. The only difference is that not all take the same methods of showing it.”
—François, Duc De La Rochefoucauld (16131680)