Community Tech Days C# 4.0 - Presentation Transcript
C# 4.0 Language Enhancements Sankarsan Bose 11th October 2009
Agenda
Journey of C#
Current Trends
C# 4.0 Features
Dynamic Objects
Named And Optional Parameters
Covariance And Contravariance
Improved COM InterOp
Journey of C# ??? Functional + LINQ Generics Static Language + Managed Code
Current Trends 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 staticallytyped as IOfficeDocument
Dynamic Programming Dynamically 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 typedlanguage How we are making it dynamic ??? Here Comes DLR Dynamic Language Runtime
Dynamic Programming DLR 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 Programming Class Generated Under The Hood
CallSite is created before method call
Actual method call is delegated to CallSite
Compiler Generated Class
Reference to CallSite
Dynamic Programming CLR – DLR Interaction Runtime Compile Time CLR Compiler injects call to DLR CallSite C# Source Code MSIL Code Call is delegated to DLR Call Site Cache DLR CallSite Cache Determines type Determines method Returns Expression Tree Call Runtime Binder for Cache miss C# Runtime Binder
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 Solution Using C# 4.0 Optional Parameter with Default Value Optional Parameter omitted in the call Method call with parameter names Position reversed
Optional & Named Parameters Demo
Covariance & Contravariance Subclass Ellipse Circle As per Liskov Substitution Principle(LSP): Any program which expects an object of Ellipse Will function without any change with an object of Circle Covariance – Type substitution as per LSP Contravariance – Type substitution reverse to LSP
Covariance & Contravariance Delegate Covariance in C# (Already Present) Func<Base> is a delegate which returns type “Base” Func<Base> can be replaced by function with return type “Derived”
Covariance & Contravariance Delegate Contravariance in C# (Already Present) Func<Derived , Object> is a delegate which accepts type “Derived” 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 & Contravariance Contravariance 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 InterOp Pass Parameters without using “ref”
Improved COM InterOp Take advantage of named parameters & optional arguments
Improved COM InterOp Take advantage of named parameters & optional arguments
Improved COM InterOp Demo
Journey of C# - Revisited Dynamic Functional + LINQ Generics Static Language + Managed Code
0 comments
Post a comment