devLink - What's New in C# 4?
-
Upload
kevin-pilch-bisson -
Category
Technology
-
view
524 -
download
0
Transcript of devLink - What's New in C# 4?
![Page 1: devLink - What's New in C# 4?](https://reader033.fdocuments.us/reader033/viewer/2022042615/55a9a2991a28abdd338b457b/html5/thumbnails/1.jpg)
What’s new in C# 4?Kevin Pilch-Bisson
http://twitter.com/Pilchie
![Page 2: devLink - What's New in C# 4?](https://reader033.fdocuments.us/reader033/viewer/2022042615/55a9a2991a28abdd338b457b/html5/thumbnails/2.jpg)
The Evolution of C#
C# 1.0
C# 2.0
C# 3.0
Managed Code
Generics
Language Integrated Query
![Page 3: devLink - What's New in C# 4?](https://reader033.fdocuments.us/reader033/viewer/2022042615/55a9a2991a28abdd338b457b/html5/thumbnails/3.jpg)
Trends
![Page 4: devLink - What's New in C# 4?](https://reader033.fdocuments.us/reader033/viewer/2022042615/55a9a2991a28abdd338b457b/html5/thumbnails/4.jpg)
The Evolution of C#
C# 1.0
C# 2.0
C# 3.0
Managed Code
Generics
Language Integrated Query
C# 4.0Dynamic Programming
![Page 5: devLink - What's New in C# 4?](https://reader033.fdocuments.us/reader033/viewer/2022042615/55a9a2991a28abdd338b457b/html5/thumbnails/5.jpg)
C# 4.0 Language Innovations
![Page 6: devLink - What's New in C# 4?](https://reader033.fdocuments.us/reader033/viewer/2022042615/55a9a2991a28abdd338b457b/html5/thumbnails/6.jpg)
PythonBinder
RubyBinder
COMBinder
JavaScriptBinder
ObjectBinder
.NET Dynamic Programming
Dynamic Language Runtime
Expression Trees Dynamic Dispatch Call Site Caching
IronPython IronRuby C# VB.NET Others…
![Page 7: devLink - What's New in C# 4?](https://reader033.fdocuments.us/reader033/viewer/2022042615/55a9a2991a28abdd338b457b/html5/thumbnails/7.jpg)
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
![Page 8: devLink - What's New in C# 4?](https://reader033.fdocuments.us/reader033/viewer/2022042615/55a9a2991a28abdd338b457b/html5/thumbnails/8.jpg)
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
![Page 9: devLink - What's New in C# 4?](https://reader033.fdocuments.us/reader033/viewer/2022042615/55a9a2991a28abdd338b457b/html5/thumbnails/9.jpg)
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)
![Page 10: devLink - What's New in C# 4?](https://reader033.fdocuments.us/reader033/viewer/2022042615/55a9a2991a28abdd338b457b/html5/thumbnails/10.jpg)
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
![Page 11: devLink - What's New in C# 4?](https://reader033.fdocuments.us/reader033/viewer/2022042615/55a9a2991a28abdd338b457b/html5/thumbnails/11.jpg)
Dynamically Typed Objects
![Page 12: devLink - What's New in C# 4?](https://reader033.fdocuments.us/reader033/viewer/2022042615/55a9a2991a28abdd338b457b/html5/thumbnails/12.jpg)
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
![Page 13: devLink - What's New in C# 4?](https://reader033.fdocuments.us/reader033/viewer/2022042615/55a9a2991a28abdd338b457b/html5/thumbnails/13.jpg)
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
![Page 14: devLink - What's New in C# 4?](https://reader033.fdocuments.us/reader033/viewer/2022042615/55a9a2991a28abdd338b457b/html5/thumbnails/14.jpg)
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" ) ;
![Page 15: devLink - What's New in C# 4?](https://reader033.fdocuments.us/reader033/viewer/2022042615/55a9a2991a28abdd338b457b/html5/thumbnails/15.jpg)
Improved COM Interoperability
• Automatic object dynamic mapping• Optional and named parameters• Indexed properties• Optional “ref” modifier• Interop type embedding (“No PIA”)
![Page 16: devLink - What's New in C# 4?](https://reader033.fdocuments.us/reader033/viewer/2022042615/55a9a2991a28abdd338b457b/html5/thumbnails/16.jpg)
Improved COM Interoperability
![Page 17: devLink - What's New in C# 4?](https://reader033.fdocuments.us/reader033/viewer/2022042615/55a9a2991a28abdd338b457b/html5/thumbnails/17.jpg)
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
![Page 18: devLink - What's New in C# 4?](https://reader033.fdocuments.us/reader033/viewer/2022042615/55a9a2991a28abdd338b457b/html5/thumbnails/18.jpg)
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
![Page 19: devLink - What's New in C# 4?](https://reader033.fdocuments.us/reader033/viewer/2022042615/55a9a2991a28abdd338b457b/html5/thumbnails/19.jpg)
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
![Page 20: devLink - What's New in C# 4?](https://reader033.fdocuments.us/reader033/viewer/2022042615/55a9a2991a28abdd338b457b/html5/thumbnails/20.jpg)
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
![Page 21: devLink - What's New in C# 4?](https://reader033.fdocuments.us/reader033/viewer/2022042615/55a9a2991a28abdd338b457b/html5/thumbnails/21.jpg)
The Evolution of C#
C# 1.0
C# 2.0
C# 3.0
Managed Code
Generics
Language Integrated Query
C# 4.0Dynamic Programming
![Page 22: devLink - What's New in C# 4?](https://reader033.fdocuments.us/reader033/viewer/2022042615/55a9a2991a28abdd338b457b/html5/thumbnails/22.jpg)
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
![Page 23: devLink - What's New in C# 4?](https://reader033.fdocuments.us/reader033/viewer/2022042615/55a9a2991a28abdd338b457b/html5/thumbnails/23.jpg)
Compiler as a Service
![Page 24: devLink - What's New in C# 4?](https://reader033.fdocuments.us/reader033/viewer/2022042615/55a9a2991a28abdd338b457b/html5/thumbnails/24.jpg)
Questions?
![Page 25: devLink - What's New in C# 4?](https://reader033.fdocuments.us/reader033/viewer/2022042615/55a9a2991a28abdd338b457b/html5/thumbnails/25.jpg)
© 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.