C#.Net Software Development Versions 1.0
Overview Rational for Extension Methods C# Specification for Extension Methods Requirements for Extension Methods Invoking Extension Methods Importing Extension Methods Generic Extension Methods 2 Copyright © 2008 by Dennis A. Fairclough all rights reserved.
Extension Method Rational “Extension methods, make it possible to extend existing types and constructed types with additional methods.” “Extension methods have all the capabilities of regular static methods. In addition, once imported, extension methods can be invoked using instance method syntax.” Extend sealed and other classes C# 3.0 Specification 3 Copyright © 2008 by Dennis A. Fairclough all rights reserved.
Extension Methods – C# Spec Extension methods When the first parameter of a method includes the this modifier, that method is said to be an extension method. Extension methods can only be declared in non-generic, non-nested static classes. The first parameter of an extension method can have no modifiers other than this, and the parameter type cannot be a reference (pointer) type. Copyright © 2008 by Dennis A. Fairclough all rights reserved. 4
Requirements Must be in a static class Must be a static method in a static class First parameter (this, …) Example static public char LastChar(this string lhs) { return lhs[lhs.Length-1]; } char endchar = “The last string”.LastChar(); 5 Copyright © 2008 by Dennis A. Fairclough all rights reserved.
C# Spec The following is an example of a static class that declares two extension methods: public static class Extensions { public static int ToInt32(this string s) { return Int32.Parse(s); } public static T[] Slice (this T[] source, int index, int count) { if (index < 0 || count < 0 || source.Length – index < count) throw new ArgumentException(); T[] result = new T[count]; Array.Copy(source, index, result, 0, count); return result; } } Copyright © 2008 by Dennis A. Fairclough all rights reserved. 6
C# Spec An extension method is a regular static method. In addition, where its enclosing static class is in scope, an extension method can be invoked using instance method invocation syntax (§ ), using the receiver expression as the first argument. Copyright © 2008 by Dennis A. Fairclough all rights reserved. 7
C# Spec The following program uses the extension methods declared previously: static class Program { static void Main() { string[] strings = { "1", "22", "333", "4444" }; foreach (string s in strings.Slice(1, 2)) { Console.WriteLine(s.ToInt32()); } } } Copyright © 2008 by Dennis A. Fairclough all rights reserved. 8
Invoking an Extension Method char endchar = “The last string”.LastChar(); same as char endchar = ExtensionClass.LastChar(“The last string”); 9 Copyright © 2008 by Dennis A. Fairclough all rights reserved.
Importing Extension Methods Import extension method namespaces with the using syntax. Instance methods take precedence over extension methods. 10 Copyright © 2008 by Dennis A. Fairclough all rights reserved.
Generic Extension Methods Generic Form static access_specifier type ident (this, …){ … } Example static public T GenExten (this T exid){ … return exid; } 11 Copyright © 2008 by Dennis A. Fairclough all rights reserved.
What did you learn? ?? 12 Copyright © 2008 by Dennis A. Fairclough all rights reserved.