lambdas and extension using vs2010

14
Introduction to Extension Methods and Lambdas using Visual Studio 2010 Presented by: Vincent Grondin C# MVP Senior Consultant [email protected] [email protected]

Upload: vgrondin

Post on 10-May-2015

1.426 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Lambdas and Extension using VS2010

Introduction to Extension Methods and Lambdas using Visual Studio 2010Presented by: Vincent Grondin

C# MVPSenior [email protected]@hotmail.com

Page 2: Lambdas and Extension using VS2010

Presentation Agenda

1. What are extension methods?

2. How do you create and use them?

3. New ideas for Extension methods!

4. Questions on extension methods?

5. What are lambdas?

6. How do you create and use them?

7. Where can they be used?

8. Explore Lambdas and a couple Gotchas!

9. Questions on lambdas?

Page 3: Lambdas and Extension using VS2010

What are extension methods?

• Extension methods make it easier to use “Helper” style methods creating the illusion that the method is an instance method of the used type.– DateTime

• Add• Substract• ToString• GetAge -> is an extension that I created to extend or customize the

DateTime type.

• I say enough with “Helper” classes!

• Let intellisense do the hard work of finding the right helper method for the class you’re using!

• You can extend anything from “int” to “string” or “DataReader”, you can extend interfaces and even sealed types!

Page 4: Lambdas and Extension using VS2010

How do you create and use them?

• 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.

• Demo VB.NET

• Demo C#

• Security Note:– 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!

Page 5: Lambdas and Extension using VS2010

New ideas for Extension methods!

Page 6: Lambdas and Extension using VS2010

Questions?

Page 7: Lambdas and Extension using VS2010

What are lambdas?

Page 8: Lambdas and Extension using VS2010

What are lambdas?

• Lambdas are close cousins of anonymous methods

• You can only access the lambda if you placed it in a delegate, preferably of type Action, Func or Predicate.

• A lambda can declare parameters to be used inside the function’s body

• If you declare more than one parameter, you HAVE to surround them with ( ).

• You can create 2 types of Lambdas:– Lambda Expressions that can produce “expression trees”– Lambda Statements

Page 9: Lambdas and Extension using VS2010

What are lambdas?

• In C#, we use the symbol “=>” to help us DEFINE a lambda like this:

x => x == 1

• In VB.NET, we use the keyword “Function” to help us DEFINE a lambda like this:

Function(x) x = 1

• In VB.NET using VS2008 it’s impossible to define multi-line lambdas, they can only have one line of CODE. This is fixed in VB.NET using VS2010.

• When using C#, you have to enclose the body of your lambda with { } when it has more than one line of code.

Page 10: Lambdas and Extension using VS2010

How do you create and use them?

• Using VB.NET– List.Where(Function(employee) employee.Age =

34)– Dim myFunction as Predicate(Of int) = Function(x) x

= 10

• Using C#– List.Where(employee => employee.Age == 34)– Predicate<int> myFunction = x =>x == 10– List.Where( employee =>

{ employee.Age += 1; employee.Age == 34;

});

Page 11: Lambdas and Extension using VS2010

Where can they be used?

• Used extensively with LinQ “standard query operators” which are nothing more than extension methods!

• You can use lambdas whenever you see a method that takes one of these has a parameter:– “Action” or one of the different flavors of “Action<T>”– “Predicate<T>– One off the different flavors of “Func<TResult>”– Any custom delegate that’s defined in your application

(but why would create your own delegates when you have “Func”, “Action” and “Predicate” ???)

Page 12: Lambdas and Extension using VS2010

Explore Lambdas and a couple Gotchas!

Page 13: Lambdas and Extension using VS2010

Usefull links!

• http://umbrella.codeplex.com/• http://geekswithblogs.net/vincentgrondin

Page 14: Lambdas and Extension using VS2010