Introduction to Extension Methods and Lambdas using Visual Studio 2010<br />Presented by: 		Vincent Grondin<br />			C# MVP...
Presentation Agenda<br />What are extension methods?<br />How do you create and use them?<br />New ideas for Extension met...
What are extension methods?<br />Extension methods make it easier to use “Helper” style methods creating the illusion that...
How do you create and use them?<br />You use an extension method just like you would use an instance method.  In facts som...
New ideas for Extension methods!<br />
Questions?<br />
What are lambdas?<br />
What are lambdas?<br />Lambdas are close cousins of anonymous methods<br />You can only access the lambda if you placed it...
What are lambdas?<br />In C#, we use the symbol “=>” to help us DEFINE a lambda like this:<br />		x => x == 1<br />In VB.N...
How do you create and use them?<br />Using VB.NET<br />List.Where(Function(employee) employee.Age = 34)<br />Dim myFunctio...
Where can they be used?<br />Used extensively with LinQ “standard query operators” which are nothing more than extension m...
Explore Lambdas and a couple Gotchas!<br />
Usefull links!<br />http://umbrella.codeplex.com/<br />http://geekswithblogs.net/vincentgrondin<br />
Lambdas and Extension using VS2010
Upcoming SlideShare
Loading in...5
×

Lambdas and Extension using VS2010

1,434

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,434
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
13
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Lambdas and Extension using VS2010

  1. 1. Introduction to Extension Methods and Lambdas using Visual Studio 2010<br />Presented by: Vincent Grondin<br /> C# MVP<br /> Senior Consultant<br />vincent_grondin@dmr.ca<br /> vgrondin@hotmail.com<br />
  2. 2. Presentation Agenda<br />What are extension methods?<br />How do you create and use them?<br />New ideas for Extension methods! <br />Questions on extension methods?<br />What are lambdas?<br />How do you create and use them? <br />Where can they be used?<br />Explore Lambdas and a couple Gotchas!<br />Questions on lambdas?<br />
  3. 3. What are extension methods?<br />Extension methods make it easier to use “Helper” style methods creating the illusion that the method is an instance method of the used type.<br />DateTime<br />Add<br />Substract<br />ToString<br />GetAge -> is an extension that I created to extend or customize the DateTime type.<br />I say enough with “Helper” classes!<br />Let intellisense do the hard work of finding the right helper method for the class you’re using!<br />You can extend anything from “int” to “string” or “DataReader”, you can extend interfaces and even sealed types!<br />
  4. 4. How do you create and use them?<br />You use an extension method just like you would use an instance method. In facts sometimes you may not even realize you’re using an extension method on a given type.<br />Demo VB.NET<br />Demo C#<br />Security Note:<br />When a given type has an extension and an instance method with the same name and signature and a call is made, the instance method will always be the one called. There’s no security issue with extension methods!<br />
  5. 5. New ideas for Extension methods!<br />
  6. 6. Questions?<br />
  7. 7. What are lambdas?<br />
  8. 8. What are lambdas?<br />Lambdas are close cousins of anonymous methods<br />You can only access the lambda if you placed it in a delegate, preferably of type Action, Func or Predicate.<br />A lambda can declare parameters to be used inside the function’s body<br />If you declare more than one parameter, you HAVE to surround them with ( ).<br />You can create 2 types of Lambdas:<br />Lambda Expressions that can produce “expression trees”<br />Lambda Statements<br />
  9. 9. What are lambdas?<br />In C#, we use the symbol “=>” to help us DEFINE a lambda like this:<br /> x => x == 1<br />In VB.NET, we use the keyword “Function” to help us DEFINE a lambda like this:<br /> Function(x) x = 1<br />In VB.NET using VS2008 it’s impossible to define multi-line lambdas, theycanonly have one line of CODE. This isfixed in VB.NET using VS2010.<br />When using C#, you have to enclose the body of your lambda with { } when it has more than one line of code.<br />
  10. 10. How do you create and use them?<br />Using VB.NET<br />List.Where(Function(employee) employee.Age = 34)<br />Dim myFunction as Predicate(Of int) = Function(x) x = 10<br />Using C#<br />List.Where(employee => employee.Age == 34)<br />Predicate<int> myFunction = x =>x == 10<br />List.Where( employee => <br />{<br />employee.Age += 1;<br />employee.Age == 34;<br />});<br />
  11. 11. Where can they be used?<br />Used extensively with LinQ “standard query operators” which are nothing more than extension methods!<br />You can use lambdas whenever you see a method that takes one of these has a parameter:<br />“Action” or one of the different flavors of “Action<T>”<br />“Predicate<T><br />One off the different flavors of “Func<TResult>”<br />Any custom delegate that’s defined in your application (but why would create your own delegates when you have “Func”, “Action” and “Predicate” ???)<br />
  12. 12. Explore Lambdas and a couple Gotchas!<br />
  13. 13. Usefull links!<br />http://umbrella.codeplex.com/<br />http://geekswithblogs.net/vincentgrondin<br />
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×