Tech·Ed North America 2009 11/18/2018 5:14 PM © 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. 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.
The Future of C# Anders Hejlsberg Technical Fellow Microsoft DTL331 Tech·Ed North America 2009 11/18/2018 5:14 PM The Future of C# Anders Hejlsberg Technical Fellow Microsoft DTL331 © 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. 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.
The Evolution of C# C# 3.0 C# 2.0 C# 1.0 Language Integrated Query Generics C# 1.0 Managed Code
Trends Declarative Concurrent Dynamic Move towards more declarative styles of programming Domain specific, Functional Resurgence of dynamic programming languages Meta-programming Need for better concurrent programming models Distributed applications Many-core Trends cause fusing of ideas from previously distinct disciplines Functional in mainstream languages Static typing in dynamic languages Implicit typing in static languages Domain specific in every app HTML, SQL, XAML, XSLT The classic taxonomies are breaking down Going forward, mainstream GPLs will be multi-paradigm!
Declarative Programming What How Imperative Declarative
Dynamic vs. Static Dynamic Languages Static Languages Simple and succinct Implicitly typed Meta-programming No compilation Static Languages Robust Performant Intelligent tools Better scaling Dynamic languages have seen a notable resurgence in recent years In browser, this is perhaps more by necessity than by choice But even in traditional static language environments… Interesting to contrast advantages Dynamic languages Aim to be simple and succinct Interactive prompt where you can write one-liners that do useful work No strong typing to “get in your way” Strong meta-programming facilities (Eval) Compilation is implicit—just type your code and run. Static languages seek to be more robust. Compile-time errors vs. run-time errors Strong typing Statement completion, refactoring, navigation, dependency analysis Performance better because run-time checks can be avoided Often scale better to medium and large projects. In today’s environment, hard to choose The answer is both! That’s why they continue to borrow from each other Interestingly, dynamic language features often have static counterparts Implicit typing vs. type inference Extensible objects vs. extension methods
Concurrency
Co-Evolution
The Evolution of C# C# 4.0 C# 3.0 C# 2.0 C# 1.0 Dynamic Programming Language Integrated Query C# 2.0 Generics C# 1.0 Managed Code
C# 4.0 Language Innovations Dynamically Typed Objects Optional and Named Parameters Improved COM Interoperability Co- and Contra-variance
.NET Dynamic Programming IronPython IronRuby C# VB.NET Others… Dynamic Language Runtime Expression Trees Dynamic Dispatch Call Site Caching Object Binder JavaScript Binder Python Binder Ruby Binder COM Binder
Dynamically Typed Objects Calculator calc = GetCalculator(); int sum = calc.Add(10, 20); object calc = GetCalculator(); Type calcType = calc.GetType(); object res = calcType.InvokeMember("Add", BindingFlags.InvokeMethod, null, new object[] { 10, 20 }); int sum = Convert.ToInt32(res); ScriptObject calc = GetCalculator(); object res = calc.Invoke("Add", 10, 20); int sum = Convert.ToInt32(res); Statically typed to be dynamic dynamic calc = GetCalculator(); int sum = calc.Add(10, 20); Dynamic conversion Dynamic method invocation
Dynamically Typed Objects Compile-time type dynamic Run-time type System.Int32 dynamic x = 1; dynamic y = "Hello"; dynamic z = new List<int> { 1, 2, 3 }; When operand(s) are dynamic… Member selection deferred to run-time At run-time, actual type(s) substituted for dynamic Static result type of operation is dynamic
Dynamically Typed Objects public static class Math { public static decimal Abs(decimal value); public static double Abs(double value); public static float Abs(float value); public static int Abs(int value); public static long Abs(long value); public static sbyte Abs(sbyte value); public static short Abs(short value); ... } Method chosen at compile-time: double Abs(double x) double x = 1.75; double y = Math.Abs(x); Method chosen at run-time: double Abs(double x) dynamic x = 1.75; dynamic y = Math.Abs(x); Method chosen at run-time: int Abs(int x) dynamic x = 2; dynamic y = Math.Abs(x);
Dynamically Typed Objects 11/18/2018 5:14 PM demo If you would like to host your demo on the Virtual Server, please use the myVPC demo slide, not this slide. Dynamically Typed Objects © 2007 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.
IDynamicObject public abstract class DynamicObject : IDynamicObject { public virtual object GetMember(GetMemberBinder info); public virtual object SetMember(SetMemberBinder info, object value); public virtual object DeleteMember(DeleteMemberBinder info); public virtual object UnaryOperation(UnaryOperationBinder info); public virtual object BinaryOperation(BinaryOperationBinder info, object arg); public virtual object Convert(ConvertBinder info); public virtual object Invoke(InvokeBinder info, object[] args); public virtual object InvokeMember(InvokeMemberBinder info, object[] args); public virtual object CreateInstance(CreateInstanceBinder info, object[] args); public virtual object GetIndex(GetIndexBinder info, object[] indices); public virtual object SetIndex(SetIndexBinder info, object[] indices, object value); public virtual object DeleteIndex(DeleteIndexBinder info, object[] indices); public MetaObject IDynamicObject.GetMetaObject(); }
Implementing IDynamicObject 11/18/2018 5:14 PM demo If you would like to host your demo on the Virtual Server, please use the myVPC demo slide, not this slide. Implementing IDynamicObject © 2007 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.
Optional and Named Parameters Primary method public StreamReader OpenTextFile( string path, Encoding encoding, bool detectEncoding, int bufferSize); Secondary overloads public StreamReader OpenTextFile( string path, Encoding encoding, bool detectEncoding); Encoding encoding); string path); Call primary with default values
Optional and Named Parameters Optional parameters public StreamReader OpenTextFile( string path, Encoding encoding = null, bool detectEncoding = true, int bufferSize = 1024); public StreamReader OpenTextFile( string path, Encoding encoding, bool detectEncoding, int bufferSize); Named argument OpenTextFile("foo.txt", Encoding.UTF8); OpenTextFile("foo.txt", Encoding.UTF8, bufferSize: 4096); Named arguments can appear in any order Arguments evaluated in order written Named arguments must be last OpenTextFile( bufferSize: 4096, path: "foo.txt", detectEncoding: false); Non-optional must be specified
Improved COM Interoperability object fileName = "Test.docx"; object missing = System.Reflection.Missing.Value; doc.SaveAs(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); 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 11/18/2018 5:14 PM demo If you would like to host your demo on the Virtual Server, please use the myVPC demo slide, not this slide. Improved COM interoperability © 2007 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.
Co- and Contra-variance .NET arrays are co-variant string[] strings = GetStringArray(); Process(strings); …but not safely co-variant void Process(object[] objects) { objects[0] = "Hello"; // Ok objects[1] = new Button(); // Exception! } void Process(object[] objects) { … } Until now, C# generics have been invariant List<string> strings = GetStringList(); Process(strings); C# 4.0 supports safe co- and contra-variance void Process(IEnumerable<object> objects) { … } void Process(IEnumerable<object> objects) { // IEnumerable<T> is read-only and // therefore safely co-variant }
Safe Co- and Contra-variance public interface IEnumerable<out T> { IEnumerator<T> GetEnumerator(); } public interface IEnumerable<T> { IEnumerator<T> GetEnumerator(); } out = Co-variant Output positions only Can be treated as less derived public interface IEnumerator<out T> { T Current { get; } bool MoveNext(); } public interface IEnumerator<T> { T Current { get; } bool MoveNext(); } IEnumerable<string> strings = GetStrings(); IEnumerable<object> objects = strings; in = Contra-variant Input positions only public interface IComparer<T> { int Compare(T x, T y); } public interface IComparer<in T> { int Compare(T x, T y); } Can be treated as more derived IComparer<object> objComp = GetComparer(); IComparer<string> strComp = objComp;
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 Interfaces System.Collections.Generic.IEnumerable<out T> System.Collections.Generic.IEnumerator<out T> System.Linq.IQueryable<out T> System.Collections.Generic.IComparer<in T> System.Collections.Generic.IEqualityComparer<in T> System.IComparable<in T> Delegates System.Func<in T, …, out R> System.Action<in T, …> System.Predicate<in T> System.Comparison<in T> System.EventHandler<in T>
Compiler as a Service Compiler Compiler Meta-programming Class Field public Foo private string X Meta-programming Read-Eval-Print Loop Language Object Model DSL Embedding Compiler Compiler Source File .NET Assembly Source code Source code Source code Source code
question & answer Meet me at Ask-the-Experts in the TLC! Tuesday 1:15-3:15
Resources Required Slide Speakers, www.microsoft.com/teched TechEd 2009 is not producing a DVD. Please announce that attendees can access session recordings at TechEd Online. Resources www.microsoft.com/teched Sessions On-Demand & Community www.microsoft.com/learning Microsoft Certification & Training Resources http://microsoft.com/technet Resources for IT Professionals http://microsoft.com/msdn Resources for Developers www.microsoft.com/learning Microsoft Certification and Training Resources
Resources C# 4.0 Samples and Whitepaper Video: The Future of C# http://code.msdn.microsoft.com/csharpfuture Video: The Future of C# http://channel9.msdn.com/pdc2008/TL16/ Visual C# Developer Center http://csharp.net
Related Content Future Directions for Visual Basic (DTL336) Wed 5/13 | 8:30 AM-9:45 AM | Room 152 Anders Hejlsberg, Jonathan Aneja Essential LINQ with C# (DTL313) Wed 5/13 | 2:45 PM-4:00 PM | Room 402 Mark Michaelis How LINQ Works: A Deep Dive into the Visual Basic and C# Implementations (DTL402) Thu 5/14 | 2:45 PM-4:00 PM | Room 515B Jonathan Aneja Microsoft Visual C# IDE Tips and Tricks (DTL322) Fri 5/15 | 10:45 AM-12:00 PM | Petree Hall C DJ Park The State of Dynamic Languages on the Microsoft .NET Framework (DTL332) Fri 5/15 | 1:00 PM-2:15 PM | Room 404 John Lam
Complete an evaluation on CommNet and enter to win! Required Slide Complete an evaluation on CommNet and enter to win!
Required Slide 11/18/2018 5:14 PM © 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. 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. © 2007 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.