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