CSE459_CSharp_04_ExtensionLambda

CSE459_CSharp_04_ExtensionLambda - Programming in C...

Info icon This 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
Image of page 1

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

View Full Document Right 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
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; } }
Image of page 3

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

View Full Document Right 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.
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
Image of page 5

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

View Full Document Right 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
Image of page 6
Programming in C# Extension Methods CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis
Image of page 7

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

View Full Document Right Arrow Icon
Programming in C# Types - Misc CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis
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.
Image of page 9

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

View Full Document Right Arrow Icon
Initializers l Can initialize fields or properties. l new C(1, 2) {Name=“ my class ”}; l Works if public field or a property with public set l Point a = new Point { X = 0, Y = 1 }; l Can be nested (eg. Rectangle with two Points) l Collection initializers l List< int > digits = new List< int > { 0, 1}; l Must implement System.Generic.ICollection<T>
Image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern