C# 4.0 Language Enhancements<br />Sankarsan Bose<br />11th October 2009<br />
Agenda<br /><ul><li> Journey of C#
 Current Trends
 C# 4.0 Features
 Dynamic Objects
 Named And Optional Parameters
 Covariance And Contravariance
 Improved COM InterOp </li></li></ul><li>Journey of C#<br />???<br />Functional + LINQ<br />Generics<br />Static Language ...
Current Trends<br />C#<br />
C# 4.0 Features<br /><ul><li> Dynamic Programming
 Optional and Named Parameters
 Co- and Contra-variance
 Improved COM Interoperability </li></li></ul><li>Dynamic Programming<br />Ruby Code Sample<br /><ul><li>  No type is defi...
  Stores string value
  “+” operator concatenates
Assigned int values
“+” operator  adds</li></ul>Type  Determined At Runtime Based On Type Of Value Stored<br />
Dynamic Programming<br />Ruby Code Sample<br /><ul><li>  Both classes define “print”
  Exactly same signature
  No type defined for parameter “p”
  Any object supporting method “print”  is a valid input
This is called “Duck Typing”</li></ul>Method To Be Invoked Is Decided At Runtime<br />
Dynamic Programming<br />Dynamic Method Dispatch in C# (Before 4.0)<br />Both are staticallytyped as IOfficeDocument<br />
Dynamic Programming<br />Dynamically Typed Objects in C# 4.0<br /><ul><li>  New keyword  “dynamic”
 Statically Typed as “dynamic”
 Actual type determined at runtime</li></ul>Dynamic Method Invocation<br />
Dynamic Programming<br />Pros<br /><ul><li> No risk of brittle class hierarchy
 Need not implement all methods of interface
 More flexible , highly productive</li></ul>Cons<br /><ul><li> Developer needs thorough understanding of codebase
 Misuse might lead to poor maintainability</li></li></ul><li>Dynamic Programming<br />Demo<br />
Dynamic Programming<br />C# is a statically typedlanguage<br />How we are making it dynamic ???<br />Here Comes DLR<br />D...
Dynamic Programming<br />DLR is additional libraries to fit the dynamic languages into the CLR<br /><ul><li> Store program...
 Caching mechanism for dynamic call
 Provide language specific support</li></li></ul><li>Dynamic Programming<br />Class Generated Under The Hood<br /><ul><li>...
Actual method call is delegated to CallSite
Upcoming SlideShare
Loading in …5
×

Community Tech Days C# 4.0

1,704 views

Published on

My Community Tech Days presentation on C# 4.0

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

No Downloads
Views
Total views
1,704
On SlideShare
0
From Embeds
0
Number of Embeds
42
Actions
Shares
0
Downloads
43
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Community Tech Days C# 4.0

  1. 1. C# 4.0 Language Enhancements<br />Sankarsan Bose<br />11th October 2009<br />
  2. 2. Agenda<br /><ul><li> Journey of C#
  3. 3. Current Trends
  4. 4. C# 4.0 Features
  5. 5. Dynamic Objects
  6. 6. Named And Optional Parameters
  7. 7. Covariance And Contravariance
  8. 8. Improved COM InterOp </li></li></ul><li>Journey of C#<br />???<br />Functional + LINQ<br />Generics<br />Static Language + Managed Code<br />
  9. 9. Current Trends<br />C#<br />
  10. 10. C# 4.0 Features<br /><ul><li> Dynamic Programming
  11. 11. Optional and Named Parameters
  12. 12. Co- and Contra-variance
  13. 13. Improved COM Interoperability </li></li></ul><li>Dynamic Programming<br />Ruby Code Sample<br /><ul><li> No type is defined
  14. 14. Stores string value
  15. 15. “+” operator concatenates
  16. 16. Assigned int values
  17. 17. “+” operator adds</li></ul>Type Determined At Runtime Based On Type Of Value Stored<br />
  18. 18. Dynamic Programming<br />Ruby Code Sample<br /><ul><li> Both classes define “print”
  19. 19. Exactly same signature
  20. 20. No type defined for parameter “p”
  21. 21. Any object supporting method “print” is a valid input
  22. 22. This is called “Duck Typing”</li></ul>Method To Be Invoked Is Decided At Runtime<br />
  23. 23. Dynamic Programming<br />Dynamic Method Dispatch in C# (Before 4.0)<br />Both are staticallytyped as IOfficeDocument<br />
  24. 24. Dynamic Programming<br />Dynamically Typed Objects in C# 4.0<br /><ul><li> New keyword “dynamic”
  25. 25. Statically Typed as “dynamic”
  26. 26. Actual type determined at runtime</li></ul>Dynamic Method Invocation<br />
  27. 27. Dynamic Programming<br />Pros<br /><ul><li> No risk of brittle class hierarchy
  28. 28. Need not implement all methods of interface
  29. 29. More flexible , highly productive</li></ul>Cons<br /><ul><li> Developer needs thorough understanding of codebase
  30. 30. Misuse might lead to poor maintainability</li></li></ul><li>Dynamic Programming<br />Demo<br />
  31. 31. Dynamic Programming<br />C# is a statically typedlanguage<br />How we are making it dynamic ???<br />Here Comes DLR<br />Dynamic Language Runtime<br />
  32. 32. Dynamic Programming<br />DLR is additional libraries to fit the dynamic languages into the CLR<br /><ul><li> Store program/code as data
  33. 33. Caching mechanism for dynamic call
  34. 34. Provide language specific support</li></li></ul><li>Dynamic Programming<br />Class Generated Under The Hood<br /><ul><li> CallSite is created before method call
  35. 35. Actual method call is delegated to CallSite
  36. 36. Compiler Generated Class
  37. 37. Reference to CallSite</li></li></ul><li>Dynamic Programming<br />CLR – DLR Interaction<br />Runtime<br />Compile Time<br />CLR<br />Compiler injects call to DLR CallSite <br />C# Source Code<br />MSIL Code<br />Call is delegated to DLR Call Site Cache<br />DLR<br />CallSite Cache<br />Determines type Determines method<br />Returns Expression Tree<br />Call Runtime Binder for Cache miss<br />C# Runtime Binder<br />
  38. 38. Optional & Named Parameters<br />“b” & “c” are nullable & optional<br /> Calling code has to pass null value <br />This is an issue for large no of parameters <br />
  39. 39. Optional & Named Parameters<br />Solution Using Overload in C# (Before 4.0)<br />One overload accepts only “a”<br />Another accepts “a” & “b”<br />This is not possible<br />Signature clash with the previous method<br />
  40. 40. Optional & Named Parameters<br />Solution Using C# 4.0<br />Optional Parameter with Default Value<br />Optional Parameter omitted in the call<br />Method call with parameter names<br />Position reversed<br />
  41. 41. Optional & Named Parameters<br />Demo<br />
  42. 42. Covariance & Contravariance<br />Subclass<br />Ellipse<br />Circle<br />As per Liskov Substitution Principle(LSP):<br />Any program which expects an object of Ellipse<br />Will function without any change with an object of Circle<br />Covariance – Type substitution as per LSP<br />Contravariance – Type substitution reverse to LSP<br />
  43. 43. Covariance & Contravariance<br />Delegate Covariance in C# (Already Present)<br />Func&lt;Base&gt; is a delegate which returns type “Base”<br />Func&lt;Base&gt; can be replaced by function with return type “Derived”<br />
  44. 44. Covariance & Contravariance<br />Delegate Contravariance in C# (Already Present)<br />Func&lt;Derived , Object&gt; is a delegate which accepts type “Derived”<br />Func&lt;Base&gt; can be replaced by function which accepts type “Base”<br />
  45. 45. Covariance & Contravariance<br />Covariance in Arrays (Existing in C# )<br />This covariance is allowed<br />This code compiles<br />But throws Runtime Exception<br />
  46. 46. Covariance & Contravariance<br />Invariance in Generic Types(Existing in C# )<br />This covariance is not allowed<br />This code will not compile<br />Before C# 4.0 Generic Types are Invariant<br />
  47. 47. Covariance & Contravariance<br />Covariance in Generic Types of C# 4.0<br />This covariance is allowed<br />public interface IEnumerable&lt;out T&gt; : IEnumerable{IEnumerator&lt;T&gt; GetEnumerator();}<br />
  48. 48. Covariance & Contravariance<br />Contravariance in Generic Types of C# 4.0<br />This Contravariance is allowed<br />public interface IComparer&lt;in T&gt;{ public int Compare(T left, T right);}<br />
  49. 49. Covariance & Contravariance<br />Demo<br />
  50. 50. Improved COM InterOp<br /><ul><li>Optional “ref” modifier
  51. 51. Optional and named parameters
  52. 52. Dynamic objects
  53. 53. No additional type casts needed
  54. 54. InterOp type embedding (“No PIA”)</li></li></ul><li>Improved COM InterOp<br />Pass Parameters without using “ref”<br />
  55. 55. Improved COM InterOp<br />Take advantage of named parameters & optional arguments<br />
  56. 56. Improved COM InterOp<br />Take advantage of named parameters & optional arguments<br />
  57. 57. Improved COM InterOp<br />Demo<br />
  58. 58. Journey of C# - Revisited<br />Dynamic<br />Functional + LINQ<br />Generics<br />Static Language + Managed Code<br />
  59. 59. Thank You<br />
  60. 60. References<br /><ul><li>http://channel9.msdn.com/shows/Going+Deep/Inside-C-40-dynamic-type-optional-parameters-more-COM-friendly/
  61. 61. http://channel9.msdn.com/posts/Charles/C-40-Meet-the-Design-Team/
  62. 62. http://channel9.msdn.com/shows/Going+Deep/Expert-to-Expert-Anders-Hejlsberg-The-Future-of-C/
  63. 63. http://channel9.msdn.com/pdc2008/TL16/</li>

×