Upload
kevin-pilch-bisson
View
523
Download
0
Embed Size (px)
Citation preview
What’s new in C# 4?Kevin Pilch-Bisson
http://twitter.com/Pilchie
The Evolution of C#
C# 1.0
C# 2.0
C# 3.0
Managed Code
Generics
Language Integrated Query
Trends
The Evolution of C#
C# 1.0
C# 2.0
C# 3.0
Managed Code
Generics
Language Integrated Query
C# 4.0Dynamic Programming
C# 4.0 Language Innovations
PythonBinder
RubyBinder
COMBinder
JavaScriptBinder
ObjectBinder
.NET Dynamic Programming
Dynamic Language Runtime
Expression Trees Dynamic Dispatch Call Site Caching
IronPython IronRuby C# VB.NET Others…
Dynamically Typed Objects
Cal cul at or cal c = Get Cal cul at or( ) ;i nt sum = cal c. Add( 10, 20) ;
obj ect cal c = Get Cal cul at or( ) ;Type cal cType = cal c. Get Type( ) ;obj ect res = cal cType. I nvokeMember( " Add" , Bi ndi ngFl ags. I nvokeMet hod, nul l , new obj ect [ ] { 10, 20 } ) ;i nt sum = Convert . ToI nt 32( res) ;
Scr i pt Obj ect cal c = Get Cal cul at or( ) ;obj ect res = cal c. I nvoke( " Add" , 10, 20) ;i nt sum = Convert . ToI nt 32( res) ;
dynami c cal c = Get Cal cul at or( ) ;i nt sum = cal c. Add( 10, 20) ;
Statically typed to be dynamic
Dynamic method invocation
Dynamic conversion
Dynamically Typed Objects
dynami c cal c = Get Cal cul at or( ) ;dynami c sum = cal c. Add( 10, 20) ;
Another word for System.Object…
When operand(s) are dynamic…• Member selection deferred to run-time• Dispatch through COM, object itself, or C# binder• Run-time type(s) substituted for dynamic• Compile-time type of operation is dynamic
…but with dynamic semantics
Dynamically Typed Objects
publ i c st at i c cl ass Mat h{ publ i c st at i c deci mal Abs(deci mal val ue) ; publ i c st at i c doubl e Abs(doubl e val ue) ; publ i c st at i c f l oat Abs( f l oat val ue) ; publ i c st at i c i nt Abs( i nt val ue) ; publ i c st at i c l ong Abs( l ong val ue) ; publ i c st at i c sbyt e Abs(sbyt e val ue) ; publ i c st at i c short Abs(short val ue) ; . . .}
doubl e x = 1. 75;doubl e y = Mat h. Abs(x) ;
dynami c x = 1. 75;dynami c y = Mat h. Abs(x) ;
dynami c x = 2;dynami c y = Mat h. Abs(x) ;
Method chosen at compile-time:
double Abs(double x)
Method chosen atrun-time:
double Abs(double x)
Method chosen atrun-time:
int Abs(int x)
Dynamically Typed Objects
dynami c d = Get Dynami cObj ect ( . . . ) ;
d. Foo( 10, " Hel l o" ) ; / / Met hod i nvocat i on
i nt si ze = d. Wi dt h; / / Propert y access
d. x = 25; / / Fi el d access
d[ " one" ] = d[ " t wo" ] ; / / I ndexer access
i nt i = d + 3; / / Operat or appl i cat i on
st r i ng s = d(5, 10) ; / / Del egat e i nvocat i on
Dynamically Typed Objects
Optional and Named Parameters
publ i c St reamReader OpenText Fi l e( st r i ng pat h, Encodi ng encodi ng, bool det ect Encodi ng, i nt buf f erSi ze) ;
publ i c St reamReader OpenText Fi l e( st r i ng pat h, Encodi ng encodi ng, bool det ect Encodi ng) ;
publ i c St reamReader OpenText Fi l e( st r i ng pat h, Encodi ng encodi ng) ;
publ i c St reamReader OpenText Fi l e( st r i ng pat h) ;
Primary method
Secondary overloads
Call primary with default values
Optional and Named Parameters
publ i c St reamReader OpenText Fi l e( st r i ng pat h, Encodi ng encodi ng, bool det ect Encodi ng, i nt buf f erSi ze) ;
publ i c St reamReader OpenText Fi l e( st r i ng pat h, Encodi ng encodi ng = nul l , bool det ect Encodi ng = t rue, i nt buf f erSi ze = 1024) ;
Optional parameters
OpenText Fi l e( " f oo. t xt " , Encodi ng. UTF8) ;OpenText Fi l e( " f oo. t xt " , Encodi ng. UTF8, buf f erSi ze: 4096) ;
Named argument
OpenText Fi l e( buf f erSi ze: 4096, pat h: " f oo. t xt " , det ect Encodi ng: f al se) ;
Named arguments must be last
Non-optional must be specified
Evaluated inorder written
Named arguments can be in any order
Improved COM Interoperability
obj ect f i l eName = " Test . docx" ;obj ect mi ssi ng = Syst em. Ref l ect i on. Mi ssi ng. Val ue;
doc. SaveAs( ref f i l eName, ref mi ssi ng, ref mi ssi ng, ref mi ssi ng, ref mi ssi ng, ref mi ssi ng, ref mi ssi ng, ref mi ssi ng, ref mi ssi ng, ref mi ssi ng, ref mi ssi ng, ref mi ssi ng, ref mi ssi ng, ref mi ssi ng, ref mi ssi ng, ref mi ssi ng) ;
doc. SaveAs( " Test . docx" ) ;
Improved COM Interoperability
• Automatic object dynamic mapping• Optional and named parameters• Indexed properties• Optional “ref” modifier• Interop type embedding (“No PIA”)
Improved COM Interoperability
Co- and Contra-variance
voi d Process(obj ect [ ] obj ect s) { … }
st r i ng[ ] st r i ngs = Get St r i ngArray( ) ;Process(st r i ngs) ;
voi d Process(obj ect [ ] obj ect s) { obj ect s[0] = " Hel l o" ; / / Ok obj ect s[ 1] = new But t on( ) ; / / Except i on!}
Li st <st r i ng> st r i ngs = Get St r i ngLi st ( ) ;Process(st r i ngs) ;
voi d Process( I Enumerabl e<obj ect > obj ect s) { … }
.NET arrays are co-variant
…but not safelyco-variant
Until now, C# generics have been invariant
voi d Process( I Enumerabl e<obj ect > obj ect s) { / / I Enumerabl e<T> i s read-onl y and / / t heref ore saf el y co-var i ant}
C# 4.0 supports safe co- and
contra-variance
Safe Co- and Contra-variance
publ i c i nt er f ace I Enumerabl e<T>{ I Enumerat or<T> Get Enumerat or( ) ;}
publ i c i nt er f ace I Enumerat or<T>{ T Current { get ; } bool MoveNext ( ) ;}
publ i c i nt er f ace I Enumerabl e<out T>{ I Enumerat or<T> Get Enumerat or( ) ;}
publ i c i nt er f ace I Enumerat or<out T>{ T Current { get ; } bool MoveNext ( ) ;}
out = Co-variantOutput positions only
I Enumerabl e<st r i ng> st r i ngs = Get St r i ngs( ) ;I Enumerabl e<obj ect > obj ect s = st r i ngs;
Can be treated asless derived
publ i c i nt er f ace I Comparer<T>{ i nt Compare(T x, T y) ;}
publ i c i nt er f ace I Comparer<i n T>{ i nt Compare(T x, T y) ;}
I Comparer<obj ect > obj Comp = Get Comparer( ) ;I Comparer<st r i ng> st rComp = obj Comp;
in = Contra-variantInput positions only
Can be treated asmore derived
Variance in C# 4.0
• Supported for interface and delegate types• “Statically checked definition-site variance”• Value types are always invariant
• IEnumerable<int> is not IEnumerable<object>• Similar to existing rules for arrays
• ref and out parameters need invariant type
Variance in .NET Framework 4.0
System.Collections.Generic.IEnumerable<out T>System.Collections.Generic.IEnumerable<out T>
System.Collections.Generic.IEnumerator<out T>System.Collections.Generic.IEnumerator<out T>
System.Linq.IQueryable<out T>System.Linq.IQueryable<out T>
System.Collections.Generic.IComparer<in T>System.Collections.Generic.IComparer<in T>
System.Collections.Generic.IEqualityComparer<in T>System.Collections.Generic.IEqualityComparer<in T>
System.IComparable<in T>System.IComparable<in T>
InterfacesInterfaces
System.Func<in T, …, out R>System.Func<in T, …, out R>
System.Action<in T, …>System.Action<in T, …>
System.Predicate<in T>System.Predicate<in T>
System.Comparison<in T>System.Comparison<in T>
System.EventHandler<in T>System.EventHandler<in T>
DelegatesDelegates
The Evolution of C#
C# 1.0
C# 2.0
C# 3.0
Managed Code
Generics
Language Integrated Query
C# 4.0Dynamic Programming
Compiler as a Service
CompilerCompilerSource code
Source code
SourceFile
Source codeSource code
.NET Assembly
Class
Field
public Foo
private
string
X
Meta-programming Read-Eval-Print Loop
LanguageObject Model
DSL Embedding
Compiler as a Service
Questions?
© 2010 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS,
IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.