12
EXTENSION METHODS C# .Net Software Development Versions 1.0

C#.Net Software Development Versions 1.0. Overview Rational for Extension Methods C# Specification for Extension Methods Requirements for Extension

Embed Size (px)

Citation preview

Page 1: C#.Net Software Development Versions 1.0. Overview  Rational for Extension Methods  C# Specification for Extension Methods  Requirements for Extension

EXTENSION METHODS

C# .Net Software DevelopmentVersions 1.0

Page 2: C#.Net Software Development Versions 1.0. Overview  Rational for Extension Methods  C# Specification for Extension Methods  Requirements for Extension

Overview

Rational for Extension Methods C# Specification for Extension

Methods Requirements for Extension Methods Invoking Extension Methods Importing Extension Methods Generic Extension Methods

2Copyright © 2008 by Dennis A. Fairclough all rights reserved.

Page 3: C#.Net Software Development Versions 1.0. Overview  Rational for Extension Methods  C# Specification for Extension Methods  Requirements for Extension

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 classesC# 3.0 Specification

3Copyright © 2008 by Dennis A. Fairclough all rights reserved.

Page 4: C#.Net Software Development Versions 1.0. Overview  Rational for Extension Methods  C# Specification for Extension Methods  Requirements for Extension

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

Page 5: C#.Net Software Development Versions 1.0. Overview  Rational for Extension Methods  C# Specification for Extension Methods  Requirements for Extension

Requirements

Must be in a static class Must be a static method in a static class First parameter (this <type> <ident>, …) Example

static public char LastChar(this string lhs){

return lhs[lhs.Length-1];

}

char endchar = “The last string”.LastChar();

5Copyright © 2008 by Dennis A. Fairclough all rights reserved.

Page 6: C#.Net Software Development Versions 1.0. Overview  Rational for Extension Methods  C# Specification for Extension Methods  Requirements for Extension

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<T>(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

Page 7: C#.Net Software Development Versions 1.0. Overview  Rational for Extension Methods  C# Specification for Extension Methods  Requirements for Extension

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 (§7.5.5.2), using the receiver expression as the first argument.

Copyright © 2008 by Dennis A. Fairclough all rights reserved. 7

Page 8: C#.Net Software Development Versions 1.0. Overview  Rational for Extension Methods  C# Specification for Extension Methods  Requirements for Extension

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

Page 9: C#.Net Software Development Versions 1.0. Overview  Rational for Extension Methods  C# Specification for Extension Methods  Requirements for Extension

Invoking an Extension Method

char endchar = “The last string”.LastChar();

same as

char endchar = ExtensionClass.LastChar(“The last string”);

9Copyright © 2008 by Dennis A. Fairclough all rights reserved.

Page 10: C#.Net Software Development Versions 1.0. Overview  Rational for Extension Methods  C# Specification for Extension Methods  Requirements for Extension

Importing Extension Methods Import extension method

namespaces with the using syntax. Instance methods take precedence

over extension methods.

10Copyright © 2008 by Dennis A. Fairclough all rights reserved.

Page 11: C#.Net Software Development Versions 1.0. Overview  Rational for Extension Methods  C# Specification for Extension Methods  Requirements for Extension

Generic Extension Methods

Generic Form static access_specifier type ident<T>(this <T> <ident>,

…){ … }

Example static public T GenExten<T>(this T exid){ … return exid;

}

11Copyright © 2008 by Dennis A. Fairclough all rights reserved.

Page 12: C#.Net Software Development Versions 1.0. Overview  Rational for Extension Methods  C# Specification for Extension Methods  Requirements for Extension

What did you learn?

??

12Copyright © 2008 by Dennis A. Fairclough all rights reserved.