Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.

Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.

Like this presentation? Why not share!

- Как получить 1000 у.е. в месяц, про... by Sergey Zyryanov 732 views
- Optimizing Your Computer by Joel May 815 views
- Тема 01.Маркетинг by Sergey Zyryanov 433 views
- Organize, edit and share your photos by Joel May 779 views
- Databases for Windows Phone by Chris Koenig 4327 views
- Ice by clubthecat 637 views

1,549 views

1,467 views

1,467 views

Published on

Published in:
Technology

No Downloads

Total views

1,549

On SlideShare

0

From Embeds

0

Number of Embeds

10

Shares

0

Downloads

60

Comments

0

Likes

1

No embeds

No notes for slide

- 1. C# 4.0, and Beyond<br />Chris Koenig<br />Developer Evangelist<br />Microsoft Corporation<br />chris.koenig@microsoft.com<br />http://chriskoenig.net<br />@chriskoenig<br />
- 2. The Evolution of C#<br />C# 3.0<br />Language Integrated Query<br />C# 2.0<br />Generics<br />C# 1.0<br />Managed Code<br />
- 3. Trends<br />
- 4. Declarative Programming<br />What<br />How<br />Imperative<br />Declarative<br />
- 5. Dynamic vs. Static<br />
- 6. Concurrent<br />
- 7. Co-Evolution<br />
- 8. The Evolution of C#<br />C# 4.0<br />Dynamic Programming<br />C# 3.0<br />Language Integrated Query<br />C# 2.0<br />Generics<br />C# 1.0<br />Managed Code<br />
- 9. <ul><li>Dynamically Typed Objects
- 10. Optional and Named Parameters
- 11. Improved COM Interoperability
- 12. Co- and Contra-variance</li></ul>C# 4.0 Language Innovations<br />
- 13. .NET Dynamic Programming<br />IronPython<br />IronRuby<br />C#<br />VB.NET<br />Others…<br />Dynamic Language Runtime<br />Expression Trees<br />Dynamic Dispatch<br />Call Site Caching<br />PythonBinder<br />RubyBinder<br />COMBinder<br />JavaScriptBinder<br />ObjectBinder<br />
- 14. Dynamically Typed Objects<br />Calculator calc = GetCalculator();<br />int sum = calc.Add(10, 20);<br />object calc = GetCalculator();<br />TypecalcType = calc.GetType();<br />object res = calcType.InvokeMember("Add",<br />BindingFlags.InvokeMethod, null,<br />newobject[] { 10, 20 });<br />int sum = Convert.ToInt32(res);<br />ScriptObject calc = GetCalculator();<br />object res = calc.Invoke("Add", 10, 20);<br />int sum = Convert.ToInt32(res);<br />Statically typed to be dynamic<br />dynamic calc = GetCalculator();<br />int sum = calc.Add(10, 20);<br />Dynamic method invocation<br />Dynamic conversion<br />
- 15. Dynamic überalles?<br />
- 16. Dynamically Typed Objects<br />Compile-time typedynamic<br />Run-time typeSystem.Int32<br />dynamic x = 1;<br />dynamic y = "Hello";<br />dynamic z = newList<int> { 1, 2, 3 };<br />When operand(s) are dynamic…<br /><ul><li> Member selection deferred to run-time
- 17. At run-time, actual type(s) substituted for dynamic
- 18. Static result type of operation is dynamic
- 19. It’s just like object with dynamic semantics</li></li></ul><li>public static class Math<br />{<br /> publicstaticdecimal Abs(decimal value);<br /> publicstaticdouble Abs(double value);<br /> publicstaticfloat Abs(float value);<br /> publicstaticint Abs(int value);<br /> publicstaticlong Abs(long value);<br /> publicstaticsbyte Abs(sbyte value);<br /> publicstaticshort Abs(short value);<br /> ...<br />}<br />double x = 1.75;<br />double y = Math.Abs(x);<br />dynamic x = 1.75;<br />dynamic y = Math.Abs(x);<br />Dynamically Typed Objects<br />Method chosen at compile-time:double Abs(double x)<br />Method chosen at run-time: double Abs(double x)<br />Method chosen at run-time:int Abs(int x)<br />dynamic x = 2;<br />dynamic y = Math.Abs(x);<br />
- 20. The “dynamic” keyword<br />demo <br />
- 21. DynamicObject Base Class<br />publicclassDynamicObject : IDynamicMetaObjectProvider<br />{<br />protectedDynamicObject();<br />publicvirtualIEnumerable<string> GetDynamicMemberNames();<br />publicvirtualDynamicMetaObjectGetMetaObject(Expression parameter);<br />publicvirtualboolTryBinaryOperation(BinaryOperationBinder binder, objectarg, outobject result);<br />publicvirtualboolTryConvert(ConvertBinder binder, outobject result);<br />publicvirtualboolTryCreateInstance(CreateInstanceBinder binder, object[] args, outobject result);<br />publicvirtualboolTryDeleteIndex(DeleteIndexBinder binder, object[] indexes);<br />publicvirtualboolTryDeleteMember(DeleteMemberBinder binder);<br />publicvirtualboolTryGetIndex(GetIndexBinder binder, object[] indexes, outobject result);<br />publicvirtualboolTryGetMember(GetMemberBinder binder, outobject result);<br />publicvirtualboolTryInvoke(InvokeBinder binder, object[] args, outobject result);<br />publicvirtualboolTryInvokeMember(InvokeMemberBinder binder, object[] args, outobject result);<br />publicvirtualboolTrySetIndex(SetIndexBinder binder, object[] indexes, objectvalue);<br />publicvirtualboolTrySetMember(SetMemberBinder binder, object value);<br />publicvirtualboolTryUnaryOperation(UnaryOperationBinder binder, outobject result);<br />}<br />
- 22. Roll Your Own DynamicObject<br />demo <br />
- 23. Optional and Named Parameters<br />Primary method<br />publicStreamReaderOpenTextFile(<br /> string path,<br /> Encodingencoding,<br />booldetectEncoding,<br />intbufferSize);<br />Secondary overloads<br />publicStreamReaderOpenTextFile(<br /> string path,<br /> Encodingencoding,<br />booldetectEncoding);<br />publicStreamReaderOpenTextFile(<br /> string path,<br /> Encodingencoding);<br />publicStreamReaderOpenTextFile(<br /> string path);<br />Call primary with default values<br />
- 24. Optional and Named Parameters<br />Optional parameters<br />publicStreamReaderOpenTextFile(<br /> string path,<br /> Encodingencoding,<br />booldetectEncoding,<br />intbufferSize);<br />publicStreamReaderOpenTextFile(<br /> string path,<br /> Encodingencoding = null,<br />booldetectEncoding = true,<br />intbufferSize = 1024);<br />Named argument<br />OpenTextFile("foo.txt", Encoding.UTF8);<br />OpenTextFile("foo.txt", Encoding.UTF8, bufferSize: 4096);<br />Arguments evaluated in order written<br />Named arguments can appear in any order<br />Named arguments must be last<br />OpenTextFile(<br />bufferSize: 4096,<br /> path: "foo.txt",<br />detectEncoding: false);<br />Non-optional must be specified<br />
- 25. Improved COM Interoperability<br />objectfileName = "Test.docx";<br />object missing = System.Reflection.Missing.Value;<br />doc.SaveAs(reffileName,<br />ref missing, ref missing, ref missing,<br />ref missing, ref missing, ref missing,<br />ref missing, ref missing, ref missing,<br />ref missing, ref missing, ref missing,<br />ref missing, ref missing, ref missing);<br />doc.SaveAs("Test.docx");<br />
- 26. <ul><li>Optional and named parameters
- 27. Indexed properties
- 28. Optional “ref” modifier
- 29. Automatic object dynamic mapping
- 30. Interop type embedding (“No PIA”)</li></ul>Improved COM Interoperability<br />
- 31. Improved COM Interoperability<br />demo <br />
- 32. Co- and Contra-variance<br />.NET arrays are co-variant<br />string[] strings = GetStringArray();<br />Process(strings);<br />…but not safelyco-variant<br />void Process(object[] objects) { … }<br />void Process(object[] objects) {<br /> objects[0] = "Hello"; // Ok<br /> objects[1] = newButton(); // Exception!<br />}<br />Until now, C# generics have been invariant<br />List<string> strings = GetStringList();<br />Process(strings);<br />C# 4.0 supports safe co- and contra-variance<br />void Process(IEnumerable<object> objects) { … }<br />void Process(IEnumerable<object> objects) {<br />// IEnumerable<T> is read-only and<br />// therefore safely co-variant<br />}<br />
- 33. Safe Co- and Contra-variance<br />publicinterfaceIEnumerable<T><br />{<br />IEnumerator<T> GetEnumerator();<br />}<br />publicinterfaceIEnumerable<out T><br />{<br />IEnumerator<T> GetEnumerator();<br />}<br />out= Co-variantOutput positions only<br />Can be treated asless derived<br />publicinterfaceIEnumerator<T><br />{<br /> T Current { get; }<br />boolMoveNext();<br />}<br />publicinterfaceIEnumerator<out T><br />{<br /> T Current { get; }<br />boolMoveNext();<br />}<br />IEnumerable<string> strings = GetStrings();<br />IEnumerable<object> objects = strings;<br />in= Contra-variantInput positions only<br />publicinterfaceIComparer<T><br />{<br />int Compare(T x, T y);<br />}<br />publicinterfaceIComparer<in T><br />{<br />int Compare(T x, T y);<br />}<br />Can be treated asmore derived<br />IComparer<object> objComp = GetComparer();<br />IComparer<string> strComp = objComp;<br />
- 34. Variance in C# 4.0<br />Supported for interface and delegate types<br />“Statically checked definition-site variance”<br />Value types are always invariant<br />IEnumerable<int> is not IEnumerable<object><br />Similar to existing rules for arrays<br />ref and out parameters need invariant type<br />
- 35. Variance in .NET Framework 4.0<br />Interfaces<br />System.Collections.Generic.IEnumerable<out T><br />System.Collections.Generic.IEnumerator<out T><br />System.Linq.IQueryable<out T><br />System.Collections.Generic.IComparer<in T><br />System.Collections.Generic.IEqualityComparer<in T><br />System.IComparable<in T><br />Delegates<br />System.Func<in T, …, out R><br />System.Action<in T, …><br />System.Predicate<in T><br />System.Comparison<in T><br />System.EventHandler<in T><br />
- 36. The Evolution of C#<br />C# 4.0<br />Dynamic Programming<br />C# 3.0<br />Language Integrated Query<br />C# 2.0<br />Generics<br />C# 1.0<br />Managed Code<br />
- 37. Compiler as a Service<br />Class<br />Meta-programming<br />Read-Eval-Print Loop<br />public<br />Foo<br />Field<br />Language<br />Object Model<br />DSL Embedding<br />private<br />X<br />string<br />Compiler<br />Compiler<br />SourceFile<br />.NET Assembly<br />Source code<br />Source code<br />Source code<br />Source code<br />
- 38. © 2009 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.<br />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.<br />

No public clipboards found for this slide

×
### Save the most important slides with Clipping

Clipping is a handy way to collect and organize the most important slides from a presentation. You can keep your great finds in clipboards organized around topics.

Be the first to comment