community tech days c# 4.0
DESCRIPTION
My Community Tech Days presentation on C# 4.0TRANSCRIPT
C# 4.0 Language Enhancements
Sankarsan Bose11th October 2009
Journey of C# Current Trends C# 4.0 Features Dynamic Objects Named And Optional Parameters Covariance And Contravariance Improved COM InterOp
Agenda
C#
C# 2.0
C# 3.0C# 4.0
Journey of C#
???
Static Language + Managed Code
Generics
Functional + LINQ
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#
C# 4.0 Features
Dynamic Programming Optional and Named Parameters Co- and Contra-variance Improved COM Interoperability
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
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
Dynamic Programming
Dynamic Method Dispatch in C# (Before 4.0)
Both are statically typed as IOfficeDocument
Dynamic ProgrammingDynamically Typed Objects in C# 4.0
• New keyword “dynamic” • Statically Typed as “dynamic”• Actual type determined at runtime
Dynamic Method Invocation
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
Dynamic Programming
Demo
Dynamic Programming
C# is a statically typed language
How we are making it dynamic ???
Here Comes DLR
Dynamic Language Runtime
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
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
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
Optional & Named Parameters
“b” & “c” are nullable & optional
Calling code has to pass null value
This is an issue for large no of parameters
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
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
Optional & Named Parameters
Demo
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
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”
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”
Covariance & Contravariance
Covariance in Arrays (Existing in C# )
This covariance is allowed
This code compiles
But throws Runtime Exception
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
Covariance & Contravariance
Covariance in Generic Types of C# 4.0
This covariance is allowed
public interface IEnumerable<out T> : IEnumerable{
IEnumerator<T> GetEnumerator();}
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);}
Covariance & Contravariance
Demo
Improved COM InterOp
Optional “ref” modifier
Optional and named parameters
Dynamic objects No additional type casts needed
InterOp type embedding (“No PIA”)
Improved COM InterOpPass Parameters without using “ref”
Improved COM InterOpTake advantage of named parameters & optional arguments
Improved COM InterOpTake advantage of named parameters & optional arguments
Improved COM InterOp
Demo
C#
C# 2.0
C# 3.0C# 4.0
Journey of C# - Revisited
Dynamic
Static Language + Managed Code
Generics
Functional + LINQ
Thank You
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/