by gavin via elephantsintheroom.org on 12/19/2007 1:15:00 AM
Extension methods allow you to create new public methods for a type without having to edit the type's code. They have been used by Microsoft in the implementation of Linq.
The following example shows how int[] has been extended with a Min() method.
using System.Linq;//without this, the int.Min extension will not be available namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int[] intArray = { 12, 24, 45 }; int i = intArray.Min();//i = 12 } } }
Extension methods allow a new compile-time error scenario: the correct assembly for a type is referenced and brought into scope through a 'using' statement
using System.IO; class Program { static void Main(string[] args) { BinaryReader b; } }
or by fully-qualifying the type
class Program { static void Main(string[] args) { System.IO.BinaryReader b; } }
but the namespace containing the extension methods that you want to use isn't in scope. The syntax for calling an extension method (that is, the same syntax as for a normal method) does not allow for full-qualification - so, when the extension method that you want to use is defined in a different namespace from the type itself, you are forced to use a 'using' statement. As far as I'm aware, this is the only situation where you are forced to use a 'using'.
In the following 'Hello World' example, the string type is extended with 2 new methods: AppendWorld and AppendAnything.
Like other C# language changes, for example generics and Linq, extension methods can reduce the number of lines of code, but increase the - already vast - number of ways of doing the simplest thing.
This code shows how extension methods work, but is not an example of when they should be used.
using System; namespace ExtensionMethodsHelloWorld { //Extension methods must be defined in a non-generic static class public static class SomeExtensionMethods//this name is arbitrary { //The first parameter in the signature of an extension method //must be preceded by the keyword 'this'. //Calling "Hello".AppendWorld() uses "Hello" as the value of //theString within the AppendWorld method. Note the empty //parenthesis in the method call. public static string AppendWorld(this string theString) { return theString + " World"; } //Second and subsequent parameters, if present, work in the //normal way - so calling "From ".AppendAnything("Gavin") //uses "From " as theString and "Gavin" as stringToAppend. public static string AppendAnything(this string theString , string stringToAppend) { return theString + stringToAppend; } } class Program { static void Main(string[] args) { Console.WriteLine("Hello".AppendWorld()); Console.WriteLine("From ".AppendAnything("Gavin")); Console.ReadLine(); } } }
Original Post: Extension Methods Hello World
The content of the postings is owned by the respective author. CSharpFeeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on CSharpFeeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.