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 in the selfishness of man’s nature—opposition to it, is [in?] his love of justice.... Repeal the Missouri compromise—repeal all compromises—repeal the declaration of independence—repeal all past history, you still can not repeal human nature. It still will be the abundance of man’s heart, that slavery extension is wrong; and out of the abundance of his heart, his mouth will continue to speak.
    Abraham Lincoln (1809–1865)

    The ancient bitter opposition to improved methods [of production] on the ancient theory that it more than temporarily deprives men of employment ... has no place in the gospel of American progress.
    Herbert Hoover (1874–1964)