C# 4.0 and .NET 4.0

4,312 views
4,251 views

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
4,312
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
71
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

C# 4.0 and .NET 4.0

  1. 1. C# 4.0 and .NET 4.0<br />Buu Nguyen, MVP (ASP.NET)<br />www.buunguyen.net/blog<br />buunguyen@kms-technology.com<br />Microsoft Confidential<br />1<br />
  2. 2. Agenda<br />Optional and Named Parameters<br />Covariance and Contravariance<br />Dynamic Binding<br />Threading with Task API<br />Task Parallel Library (TPL)<br />PLINQ<br />
  3. 3. Optional and Named Parameters<br />
  4. 4. ?Quiz<br />What are 2 issues of this API?<br />
  5. 5. Covariance and Contravariance<br />
  6. 6. ?Quiz<br />What is wrong with this code?<br />
  7. 7. Covariance Defined<br />Let’s say S is subtype of B and GT is a generic type<br />GT<S> behaves like subtype of GT<B> on assignment compatibility, i.e.<br />GT<S> s = …<br />GT<B> b = s;<br />GT<S> s = (GT<S>)b;<br />Safe only when GT doesn’t expose members “receiving” T as input (aka input-safe)<br />Microsoft Confidential<br />7<br />
  8. 8. ?Quiz<br />What is wrong with this code?<br />
  9. 9. Contravariance Defined<br />Let’s say S is subtype of B and GT is a generic type<br />GT<B> behaves like subtype of GT<S> on assignment compatibility, i.e.<br />GT<B> b = …<br />GT<S> s = b;<br />GT<B> b = (GT<B>)s;<br />Safe only when GT doesn’t expose members “returning” T as output (aka output-safe)<br />Microsoft Confidential<br />9<br />
  10. 10. Dynamic Binding<br />
  11. 11. ?Quiz<br />Write a method that prints out the Name property of an anonymous object returned by another method<br />
  12. 12. Dynamic binding means…<br />use runtime type for all bindings<br />
  13. 13. ?Quiz<br />A method summing Length properties of all parameters, regardless of actual type<br />
  14. 14. ?Quiz<br />How to builds C# objects that behave like JavaScript objects, i.e. members can be added at any time?<br />
  15. 15. ?Quiz<br />Write a class whose objects can invoke static methods of any particular type<br />Bonus #1: include non-public static methods<br />Bonus #2: ignore case<br />
  16. 16. ?Quiz<br />Write a class whose objects can be used as result-caching proxy for the method invocation of any other object<br />
  17. 17. Task Parallel Library (TPL) & PLINQ<br />
  18. 18. ?Quiz<br />Write a method calculating Fibonacci number using a thread in thread-pool and returning result to caller<br />
  19. 19. Task Parallelism Features<br />Wait on tasks<br />Support parent-child relationship<br />Exception handling<br />Cancelling tasks<br />Continuation<br />Execute one task after another<br />Microsoft Confidential<br />19<br />
  20. 20. ?Quiz<br />Write 3 methods:<br />Calculate some known Fibonacci numbers<br />Calculate Fibonacci numbers for a range<br />Calculate Fibonacci numbers for a list of specified indexes<br />Each calculation must take place in a thread in thread-pool <br />
  21. 21. Parallel Class<br />Assign delegates to a handful of Task instances, not one-to-one mapping<br />
  22. 22. ?Quiz<br />Write a method which calculate some specified Fibonacci numbers and return the sum of them to the caller<br />

×