CSE459_CSharp_04_ExtensionLambda

CSE459_CSharp_04_ExtensionLambda - Programming in C#...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
Programming in C# Extension Methods CSE 459.24 Prof. Roger Crawfis
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Extension Methods l Can add methods to other classes l Let me repeat that: Can add methods to other classes l any methods (although they look static) l only from static classes l When you import a namespace that has extensions, these are added to classes l once imported, called as usual instance methods l Local methods take precedence
Background image of page 2
Extension Methods public static class Extensions { public static int ToInt32( this string integerString) { return Int32 .Parse(integerString); } public static T[] Slice<T>( this T[] source, int startIndex, int count) { if (startIndex < 0 || count < 0 || (source.Length - startIndex) < count) throw new InvalidArgumentException (); T[] result = new T[count]; Array .Copy(source, startIndex, result, 0, count); return result; } }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Extension Methods l Properties, events and operators can not have extension methods. l Equivalent to calling the static method l Can only access public methods, etc. l Allows for chaining method calls. l More later when we talk about LINQ.
Background image of page 4
Extension Method Example public static class MyExtensions { public static IEnumerable<T> SkipLast<T>( this IEnumerable<T> source, int count) { Queue<T> saveList = new Queue<T>(); int saved = 0; foreach (T item in source) { if (saved < count) { saveList.Enqueue(item); ++saved; continue ; } saveList.Enqueue(item); yield return saveList.Dequeue(); } yield break ; } } http://blogs.msdn.com/ericwhite/archive/2008/11/14/the-skiplast-extension-method.aspx
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Extension Method Example class Program { static void Main( string [] args) { int [] a = new [] { 1, 2, 3, 4, 5 }; var b = a.SkipLast(2); foreach ( var item in b) Console .WriteLine(item); var c = new List< string >() { "one" , "two" , "three" , "four" , "five" }; var d = c.Skip(1).SkipLast(1); foreach ( var e in d) Console .WriteLine(e); } } 1 2 3 two three four
Background image of page 6
Programming in C# Extension Methods CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Programming in C# Types - Misc CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis
Background image of page 8
Initializers l We already know how to initialize basic types: int index = 0; string message = “Save the Cheerleader” ; l And complex types using constructors: Person paul = new Person( “Paul” , “Newman” ); IList< string > words = new List< string >(35); l Can also use initializers to set properties and initialize collections.
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Initializers l Can initialize fields or properties. l
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/27/2012 for the course CSE 459.24 taught by Professor Crawfis during the Winter '11 term at Ohio State.

Page1 / 34

CSE459_CSharp_04_ExtensionLambda - Programming in C#...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online