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:

    The motive of science was the extension of man, on all sides, into Nature, till his hands should touch the stars, his eyes see through the earth, his ears understand the language of beast and bird, and the sense of the wind; and, through his sympathy, heaven and earth should talk with him. But that is not our science.
    Ralph Waldo Emerson (1803–1882)

    A woman might claim to retain some of the child’s faculties, although very limited and defused, simply because she has not been encouraged to learn methods of thought and develop a disciplined mind. As long as education remains largely induction ignorance will retain these advantages over learning and it is time that women impudently put them to work.
    Germaine Greer (b. 1939)