180 likes | 359 Views
Hardcore C#: Power and Flexibility. Pavel Yosifovich CTO, Hi-Tech College http://blogs.microsoft.co.il/blogs/pavely. Quick Teaser…. What is yield ? (in C#) What does this code do? Does it compile? Why?. int yield = 6; Console.WriteLine (yield);. Agenda. Delegates & Lambdas
E N D
Hardcore C#: Power and Flexibility Pavel Yosifovich CTO, Hi-Tech College http://blogs.microsoft.co.il/blogs/pavely
Quick Teaser… • What is yield? (in C#) • What does this code do? • Does it compile? Why? int yield = 6; Console.WriteLine(yield);
Agenda • Delegates & Lambdas • Using & IDisposable • Iterators • Extension Methods • Q & A • Summary
Demo Delegates and Lambdas
Delegates And Lambdas • Anonymous Delegates / Methods • Actual method created by the compiler • Can use locals of containing method • Lambda Expressions • C# 3.0 way to express anonymous delegates
IDisposable and using • GC and finalization • Deterministic finalization with IDisposable • Use using to deterministically dispose of resources • Finalizer backups up IDisposable • Can use in other ways
Demo using and IDisposable
using Operation using(Resourceres = newResource()) { res.DoWork(); } Resourceres = newResource(...); try { res.DoWork(); } finally { IDisposabledisp = resasIDisposable; if(disp != null) disp.Dispose(); }
What is foreach? // coll implements IEnumerable<string> foreach(stringxincoll) DoSomethingWithItem(x); IEnumerator<string> en = coll.GetEnumerator(); while(en.MoveNext()) { DoSomethingWithItem(en.Current); } IDisposabledisp = collasIDisposable; if(disp!= null) disp.Dispose();
Demo Iterators
Iterators • Method returning IEnumerable<T> or IEnumerator<T> • Use yieldreturnto return and suspend execution • Use yieldbreakto terminate iteration • Can be invoked without the usual foreach
Demo Extension Methods
Extension Methods • Static methods in a static class • First argument must be prefixed with this keyword • Method “extends” first argument type • Basis of LINQ syntax
Summary • Topics covered • Delegates & Lambdas • using & IDisposable • Iterators • Extension Methods • Call to action • Explore and be creative!
Related Sessions LAB04: C# 3.0 & LINQEranSharabi 14:30 – 15:40 Golan DEV403: A Deep Dive into LINQEranSharabi 16:00 – 17:10 Galil DEV402: Concurrent ProgrammingSasha Goldshtein 12:20 – 13:30 Galil
© 2008 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.