C# 4.0 and .NET 4.0Buu Nguyen, MVP (ASP.NET)www.buunguyen.net/blogbuunguyen@kms-technology.comMicrosoft Confidential1
AgendaOptional and Named ParametersCovariance and ContravarianceDynamic BindingThreading with Task APITask Parallel Library (TPL)PLINQ
Optional and Named Parameters
?QuizWhat are 2 issues of this API?
Covariance and Contravariance
?QuizWhat is wrong with this code?
Covariance DefinedLet’s say S is subtype of B and GT is a generic typeGT<S> behaves like subtype of GT<B> on assignment compatibility, i.e.GT<S> s = …GT<B> b = s;GT<S> s = (GT<S>)b;Safe only when GT doesn’t expose members “receiving” T as input (aka input-safe)Microsoft Confidential7
?QuizWhat is wrong with this code?
Contravariance DefinedLet’s say S is subtype of B and GT is a generic typeGT<B> behaves like subtype of GT<S> on assignment compatibility, i.e.GT<B> b = …GT<S> s = b;GT<B> b = (GT<B>)s;Safe only when GT doesn’t expose members “returning” T as output (aka output-safe)Microsoft Confidential9
Dynamic Binding
?QuizWrite a method that prints out the Name property of an anonymous object returned by another method
Dynamic binding means…use runtime type for all bindings
?QuizA method summing Length properties of all parameters, regardless of actual type
?QuizHow to builds C# objects that behave like JavaScript objects, i.e. members can be added at any time?
?QuizWrite a class whose objects can invoke static methods of any particular typeBonus #1: include non-public static methodsBonus #2: ignore case
?QuizWrite a class whose objects can be used as result-caching proxy for the method invocation of any other object
Task Parallel Library (TPL) & PLINQ
?QuizWrite a method calculating Fibonacci number using a thread in thread-pool and returning result to caller
Task Parallelism FeaturesWait on tasksSupport parent-child relationshipException handlingCancelling tasksContinuationExecute one task after anotherMicrosoft Confidential19
?QuizWrite 3 methods:Calculate some known Fibonacci numbersCalculate Fibonacci numbers for a rangeCalculate Fibonacci numbers for a list of specified indexesEach calculation must take place in a thread in thread-pool
Parallel ClassAssign delegates to a handful of Task instances, not one-to-one mapping
?QuizWrite a method which calculate some specified Fibonacci numbers and return the sum of them to the caller

C# 4.0 and .NET 4.0

  • 1.
    C# 4.0 and.NET 4.0Buu Nguyen, MVP (ASP.NET)www.buunguyen.net/blogbuunguyen@kms-technology.comMicrosoft Confidential1
  • 2.
    AgendaOptional and NamedParametersCovariance and ContravarianceDynamic BindingThreading with Task APITask Parallel Library (TPL)PLINQ
  • 3.
  • 4.
    ?QuizWhat are 2issues of this API?
  • 5.
  • 6.
    ?QuizWhat is wrongwith this code?
  • 7.
    Covariance DefinedLet’s sayS is subtype of B and GT is a generic typeGT<S> behaves like subtype of GT<B> on assignment compatibility, i.e.GT<S> s = …GT<B> b = s;GT<S> s = (GT<S>)b;Safe only when GT doesn’t expose members “receiving” T as input (aka input-safe)Microsoft Confidential7
  • 8.
    ?QuizWhat is wrongwith this code?
  • 9.
    Contravariance DefinedLet’s sayS is subtype of B and GT is a generic typeGT<B> behaves like subtype of GT<S> on assignment compatibility, i.e.GT<B> b = …GT<S> s = b;GT<B> b = (GT<B>)s;Safe only when GT doesn’t expose members “returning” T as output (aka output-safe)Microsoft Confidential9
  • 10.
  • 11.
    ?QuizWrite a methodthat prints out the Name property of an anonymous object returned by another method
  • 12.
    Dynamic binding means…useruntime type for all bindings
  • 13.
    ?QuizA method summingLength properties of all parameters, regardless of actual type
  • 14.
    ?QuizHow to buildsC# objects that behave like JavaScript objects, i.e. members can be added at any time?
  • 15.
    ?QuizWrite a classwhose objects can invoke static methods of any particular typeBonus #1: include non-public static methodsBonus #2: ignore case
  • 16.
    ?QuizWrite a classwhose objects can be used as result-caching proxy for the method invocation of any other object
  • 17.
  • 18.
    ?QuizWrite a methodcalculating Fibonacci number using a thread in thread-pool and returning result to caller
  • 19.
    Task Parallelism FeaturesWaiton tasksSupport parent-child relationshipException handlingCancelling tasksContinuationExecute one task after anotherMicrosoft Confidential19
  • 20.
    ?QuizWrite 3 methods:Calculatesome known Fibonacci numbersCalculate Fibonacci numbers for a rangeCalculate Fibonacci numbers for a list of specified indexesEach calculation must take place in a thread in thread-pool
  • 21.
    Parallel ClassAssign delegatesto a handful of Task instances, not one-to-one mapping
  • 22.
    ?QuizWrite a methodwhich calculate some specified Fibonacci numbers and return the sum of them to the caller