C Sharp Syntax - Extension Methods

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:

    Slavery is founded on the selfishness of man’s nature—opposition to it on his love of justice. These principles are in eternal antagonism; and when brought into collision so fiercely as slavery extension brings them, shocks and throes and convulsions must ceaselessly follow.
    Abraham Lincoln (1809–1865)

    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 (1613–1680)