Community Tech Days C# 4.0
-
Upload
sankarsan-bose -
Category
Technology
-
view
1.431 -
download
3
description
Transcript of Community Tech Days C# 4.0
![Page 1: Community Tech Days C# 4.0](https://reader038.fdocuments.in/reader038/viewer/2022110307/55649e9dd8b42afd4f8b4eac/html5/thumbnails/1.jpg)
C# 4.0 Language Enhancements
Sankarsan Bose11th October 2009
![Page 2: Community Tech Days C# 4.0](https://reader038.fdocuments.in/reader038/viewer/2022110307/55649e9dd8b42afd4f8b4eac/html5/thumbnails/2.jpg)
Journey of C# Current Trends C# 4.0 Features Dynamic Objects Named And Optional Parameters Covariance And Contravariance Improved COM InterOp
Agenda
![Page 3: Community Tech Days C# 4.0](https://reader038.fdocuments.in/reader038/viewer/2022110307/55649e9dd8b42afd4f8b4eac/html5/thumbnails/3.jpg)
C#
C# 2.0
C# 3.0C# 4.0
Journey of C#
???
Static Language + Managed Code
Generics
Functional + LINQ
![Page 4: Community Tech Days C# 4.0](https://reader038.fdocuments.in/reader038/viewer/2022110307/55649e9dd8b42afd4f8b4eac/html5/thumbnails/4.jpg)
Current Trends
•Dynamic Typing•Dynamic Method DispatchDynamic
•Functions as first class objects•Focus on “WHAT” not “HOW”Functional
•Multi Core Computing•Message Based ConcurrencyConcurrent
C#
![Page 5: Community Tech Days C# 4.0](https://reader038.fdocuments.in/reader038/viewer/2022110307/55649e9dd8b42afd4f8b4eac/html5/thumbnails/5.jpg)
C# 4.0 Features
Dynamic Programming Optional and Named Parameters Co- and Contra-variance Improved COM Interoperability
![Page 6: Community Tech Days C# 4.0](https://reader038.fdocuments.in/reader038/viewer/2022110307/55649e9dd8b42afd4f8b4eac/html5/thumbnails/6.jpg)
Dynamic Programming
Ruby Code Sample
• No type is defined
• Stores string value
• “+” operator concatenates
• Assigned int values
• “+” operator adds
Type Determined At Runtime Based On Type Of Value Stored
![Page 7: Community Tech Days C# 4.0](https://reader038.fdocuments.in/reader038/viewer/2022110307/55649e9dd8b42afd4f8b4eac/html5/thumbnails/7.jpg)
Dynamic Programming
Ruby Code Sample
• 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”
Method To Be Invoked Is Decided At Runtime
![Page 8: Community Tech Days C# 4.0](https://reader038.fdocuments.in/reader038/viewer/2022110307/55649e9dd8b42afd4f8b4eac/html5/thumbnails/8.jpg)
Dynamic Programming
Dynamic Method Dispatch in C# (Before 4.0)
Both are statically typed as IOfficeDocument
![Page 9: Community Tech Days C# 4.0](https://reader038.fdocuments.in/reader038/viewer/2022110307/55649e9dd8b42afd4f8b4eac/html5/thumbnails/9.jpg)
Dynamic ProgrammingDynamically Typed Objects in C# 4.0
• New keyword “dynamic” • Statically Typed as “dynamic”• Actual type determined at runtime
Dynamic Method Invocation
![Page 10: Community Tech Days C# 4.0](https://reader038.fdocuments.in/reader038/viewer/2022110307/55649e9dd8b42afd4f8b4eac/html5/thumbnails/10.jpg)
Dynamic Programming
Pros
No risk of brittle class hierarchy
Need not implement all methods of interface
More flexible , highly productive
Cons
Developer needs thorough understanding of codebase
Misuse might lead to poor maintainability
![Page 11: Community Tech Days C# 4.0](https://reader038.fdocuments.in/reader038/viewer/2022110307/55649e9dd8b42afd4f8b4eac/html5/thumbnails/11.jpg)
Dynamic Programming
Demo
![Page 12: Community Tech Days C# 4.0](https://reader038.fdocuments.in/reader038/viewer/2022110307/55649e9dd8b42afd4f8b4eac/html5/thumbnails/12.jpg)
Dynamic Programming
C# is a statically typed language
How we are making it dynamic ???
Here Comes DLR
Dynamic Language Runtime
![Page 13: Community Tech Days C# 4.0](https://reader038.fdocuments.in/reader038/viewer/2022110307/55649e9dd8b42afd4f8b4eac/html5/thumbnails/13.jpg)
Dynamic ProgrammingDLR is additional libraries to fit the dynamic languages into the CLR
• Store program/code as data
• Caching mechanism for dynamic call
• Provide language specific support
![Page 14: Community Tech Days C# 4.0](https://reader038.fdocuments.in/reader038/viewer/2022110307/55649e9dd8b42afd4f8b4eac/html5/thumbnails/14.jpg)
Dynamic ProgrammingClass Generated Under The Hood
• Compiler Generated Class• Reference to CallSite
• CallSite is created before method call
•Actual method call is delegated to CallSite
![Page 15: Community Tech Days C# 4.0](https://reader038.fdocuments.in/reader038/viewer/2022110307/55649e9dd8b42afd4f8b4eac/html5/thumbnails/15.jpg)
RuntimeCLR
DLR
Compile Time
Dynamic ProgrammingCLR – DLR Interaction
C# Source Code
MSIL CodeCompiler injects call to DLR CallSite
CallSite Cache
C# Runtime Binder
Call is delegated to DLR Call Site Cache
Call Runtime Binder for Cache miss
Determines type Determines methodReturns Expression Tree
![Page 16: Community Tech Days C# 4.0](https://reader038.fdocuments.in/reader038/viewer/2022110307/55649e9dd8b42afd4f8b4eac/html5/thumbnails/16.jpg)
Optional & Named Parameters
“b” & “c” are nullable & optional
Calling code has to pass null value
This is an issue for large no of parameters
![Page 17: Community Tech Days C# 4.0](https://reader038.fdocuments.in/reader038/viewer/2022110307/55649e9dd8b42afd4f8b4eac/html5/thumbnails/17.jpg)
Optional & Named Parameters
Solution Using Overload in C# (Before 4.0)
One overload accepts only “a”
Another accepts “a” & “b”
This is not possible
Signature clash with the previous method
![Page 18: Community Tech Days C# 4.0](https://reader038.fdocuments.in/reader038/viewer/2022110307/55649e9dd8b42afd4f8b4eac/html5/thumbnails/18.jpg)
Optional & Named Parameters
Optional Parameter with Default Value
Optional Parameter omitted in the call
Method call with parameter names
Position reversed
Solution Using C# 4.0
![Page 19: Community Tech Days C# 4.0](https://reader038.fdocuments.in/reader038/viewer/2022110307/55649e9dd8b42afd4f8b4eac/html5/thumbnails/19.jpg)
Optional & Named Parameters
Demo
![Page 20: Community Tech Days C# 4.0](https://reader038.fdocuments.in/reader038/viewer/2022110307/55649e9dd8b42afd4f8b4eac/html5/thumbnails/20.jpg)
Covariance & Contravariance
Ellipse
As per Liskov Substitution Principle(LSP):Any program which expects an object of EllipseWill function without any change with an object of Circle
CircleSubclass
Covariance – Type substitution as per LSP
Contravariance – Type substitution reverse to LSP
![Page 21: Community Tech Days C# 4.0](https://reader038.fdocuments.in/reader038/viewer/2022110307/55649e9dd8b42afd4f8b4eac/html5/thumbnails/21.jpg)
Covariance & Contravariance
Func<Base> is a delegate which returns type “Base”
Delegate Covariance in C# (Already Present)
Func<Base> can be replaced by function with return type “Derived”
![Page 22: Community Tech Days C# 4.0](https://reader038.fdocuments.in/reader038/viewer/2022110307/55649e9dd8b42afd4f8b4eac/html5/thumbnails/22.jpg)
Covariance & Contravariance
Func<Derived , Object> is a delegate which accepts type “Derived”
Delegate Contravariance in C# (Already Present)
Func<Base> can be replaced by function which accepts type “Base”
![Page 23: Community Tech Days C# 4.0](https://reader038.fdocuments.in/reader038/viewer/2022110307/55649e9dd8b42afd4f8b4eac/html5/thumbnails/23.jpg)
Covariance & Contravariance
Covariance in Arrays (Existing in C# )
This covariance is allowed
This code compiles
But throws Runtime Exception
![Page 24: Community Tech Days C# 4.0](https://reader038.fdocuments.in/reader038/viewer/2022110307/55649e9dd8b42afd4f8b4eac/html5/thumbnails/24.jpg)
Covariance & Contravariance
Invariance in Generic Types(Existing in C# )
This covariance is not allowed
This code will not compile
Before C# 4.0 Generic Types are Invariant
![Page 25: Community Tech Days C# 4.0](https://reader038.fdocuments.in/reader038/viewer/2022110307/55649e9dd8b42afd4f8b4eac/html5/thumbnails/25.jpg)
Covariance & Contravariance
Covariance in Generic Types of C# 4.0
This covariance is allowed
public interface IEnumerable<out T> : IEnumerable{
IEnumerator<T> GetEnumerator();}
![Page 26: Community Tech Days C# 4.0](https://reader038.fdocuments.in/reader038/viewer/2022110307/55649e9dd8b42afd4f8b4eac/html5/thumbnails/26.jpg)
Covariance & ContravarianceContravariance in Generic Types of C# 4.0
This Contravariance is allowed
public interface IComparer<in T>{
public int Compare(T left, T right);}
![Page 27: Community Tech Days C# 4.0](https://reader038.fdocuments.in/reader038/viewer/2022110307/55649e9dd8b42afd4f8b4eac/html5/thumbnails/27.jpg)
Covariance & Contravariance
Demo
![Page 28: Community Tech Days C# 4.0](https://reader038.fdocuments.in/reader038/viewer/2022110307/55649e9dd8b42afd4f8b4eac/html5/thumbnails/28.jpg)
Improved COM InterOp
Optional “ref” modifier
Optional and named parameters
Dynamic objects No additional type casts needed
InterOp type embedding (“No PIA”)
![Page 29: Community Tech Days C# 4.0](https://reader038.fdocuments.in/reader038/viewer/2022110307/55649e9dd8b42afd4f8b4eac/html5/thumbnails/29.jpg)
Improved COM InterOpPass Parameters without using “ref”
![Page 30: Community Tech Days C# 4.0](https://reader038.fdocuments.in/reader038/viewer/2022110307/55649e9dd8b42afd4f8b4eac/html5/thumbnails/30.jpg)
Improved COM InterOpTake advantage of named parameters & optional arguments
![Page 31: Community Tech Days C# 4.0](https://reader038.fdocuments.in/reader038/viewer/2022110307/55649e9dd8b42afd4f8b4eac/html5/thumbnails/31.jpg)
Improved COM InterOpTake advantage of named parameters & optional arguments
![Page 32: Community Tech Days C# 4.0](https://reader038.fdocuments.in/reader038/viewer/2022110307/55649e9dd8b42afd4f8b4eac/html5/thumbnails/32.jpg)
Improved COM InterOp
Demo
![Page 33: Community Tech Days C# 4.0](https://reader038.fdocuments.in/reader038/viewer/2022110307/55649e9dd8b42afd4f8b4eac/html5/thumbnails/33.jpg)
C#
C# 2.0
C# 3.0C# 4.0
Journey of C# - Revisited
Dynamic
Static Language + Managed Code
Generics
Functional + LINQ
![Page 34: Community Tech Days C# 4.0](https://reader038.fdocuments.in/reader038/viewer/2022110307/55649e9dd8b42afd4f8b4eac/html5/thumbnails/34.jpg)
Thank You
![Page 35: Community Tech Days C# 4.0](https://reader038.fdocuments.in/reader038/viewer/2022110307/55649e9dd8b42afd4f8b4eac/html5/thumbnails/35.jpg)
References
http://channel9.msdn.com/shows/Going+Deep/Inside-C-40-dynamic-type-optional-parameters-more-COM-friendly/
http://channel9.msdn.com/posts/Charles/C-40-Meet-the-Design-Team/
http://channel9.msdn.com/shows/Going+Deep/Expert-to-Expert-Anders-Hejlsberg-The-Future-of-C/
http://channel9.msdn.com/pdc2008/TL16/