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:

    Where there is reverence there is fear, but there is not reverence everywhere that there is fear, because fear presumably has a wider extension than reverence.
    Socrates (469–399 B.C.)

    A writer who writes, “I am alone” ... can be considered rather comical. It is comical for a man to recognize his solitude by addressing a reader and by using methods that prevent the individual from being alone. The word alone is just as general as the word bread. To pronounce it is to summon to oneself the presence of everything the word excludes.
    Maurice Blanchot (b. 1907)