community tech days c# 4.0

35
C# 4.0 Language Enhancements Sankarsan Bose 11 th October 2009

Upload: sankarsan-bose

Post on 26-May-2015

1.432 views

Category:

Technology


3 download

DESCRIPTION

My Community Tech Days presentation on C# 4.0

TRANSCRIPT

Page 1: Community Tech Days C# 4.0

C# 4.0 Language Enhancements

Sankarsan Bose11th October 2009

Page 2: Community Tech Days C# 4.0

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

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

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

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

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

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

Dynamic Programming

Dynamic Method Dispatch in C# (Before 4.0)

Both are statically typed as IOfficeDocument

Page 9: Community Tech Days C# 4.0

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

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

Dynamic Programming

Demo

Page 12: Community Tech Days C# 4.0

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

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

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

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

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

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

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

Optional & Named Parameters

Demo

Page 20: Community Tech Days C# 4.0

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

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

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

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

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

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

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

Covariance & Contravariance

Demo

Page 28: Community Tech Days C# 4.0

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

Improved COM InterOpPass Parameters without using “ref”

Page 30: Community Tech Days C# 4.0

Improved COM InterOpTake advantage of named parameters & optional arguments

Page 31: Community Tech Days C# 4.0

Improved COM InterOpTake advantage of named parameters & optional arguments

Page 32: Community Tech Days C# 4.0

Improved COM InterOp

Demo

Page 33: Community Tech Days C# 4.0

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

Thank You