Presentation is loading. Please wait.

Presentation is loading. Please wait.

C# in the Big World Mads Torgersen Program Manager

Similar presentations


Presentation on theme: "C# in the Big World Mads Torgersen Program Manager"— Presentation transcript:

1 C# in the Big World Mads Torgersen Program Manager
Tech Ed North America 2010 9/13/2018 1:09 PM Required Slide SESSION CODE: DEV404 C# in the Big World Mads Torgersen Program Manager Microsoft Corporation © 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.

2 C# in the Big World New features The design of dynamic Dynamic
Named and optional arguments COM interop features The design of dynamic

3 The DLR on the CLR Common Language Runtime – CLR:
Common platform for static languages Facilitates great interop

4 The DLR on the CLR Common Language Runtime – CLR:
Common platform for static languages Facilitates great interop Dynamic Language Runtime – DLR: Common platform for dynamic languages

5 The DLR on the CLR Common Language Runtime – CLR:
Common platform for static languages Facilitates great interop Dynamic Language Runtime – DLR: Common platform for dynamic languages

6 Dynamic Objects Implement their own binding
Programmatically describes meaning of operations The DLR caches and optimizes Built by dynamic languages – or by you!

7 Why Dynamic in C#? Build on DLR opportunity
Use code from dynamic languages Use other dynamic object models Improve COM interop

8 Dynamic Language Runtime (DLR)
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

9 The dynamic Type in C# dynamic calc = GetCalculator();
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

10 DEMO Dynamic Tech Ed North America 2010 9/13/2018 1:09 PM
© 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.

11 System.Dynamic.DynamicObject
public class DynamicObject : IDynamicMetaObjectProvider { public virtual IEnumerable<string> GetDynamicMemberNames(); public virtual DynamicMetaObject GetMetaObject(Expression parameter); public virtual bool TryBinaryOperation(BinaryOperationBinder binder, object arg, out object result); public virtual bool TryConvert(ConvertBinder binder, out object result); public virtual bool TryCreateInstance(CreateInstanceBinder binder, object[] args, out object result); public virtual bool TryDeleteIndex(DeleteIndexBinder binder, object[] indexes); public virtual bool TryDeleteMember(DeleteMemberBinder binder); public virtual bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result); public virtual bool TryGetMember(GetMemberBinder binder, out object result); public virtual bool TryInvoke(InvokeBinder binder, object[] args, out object result); public virtual bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result); public virtual bool TrySetIndex(SetIndexBinder binder, object[] indexes, object value); public virtual bool TrySetMember(SetMemberBinder binder, object value); public virtual bool TryUnaryOperation(UnaryOperationBinder binder, out object result); }

12 DEMO DynamicObject Tech Ed North America 2010 9/13/2018 1:09 PM
© 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.

13 Optional and Named Parameters
public StreamReader OpenTextFile( string path, Encoding encoding, bool detectEncoding, int bufferSize); Primary method Secondary overloads public StreamReader OpenTextFile( string path, Encoding encoding, bool detectEncoding); Encoding encoding); string path); Call primary with default values

14 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 be in any order Evaluated in order written Named arguments must be last OpenTextFile( bufferSize: 4096, path: "foo.txt", detectEncoding: false); Non-optional must be specified

15 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");

16 Improved COM Interoperability
Optional and named parameters Indexed properties Optional “ref” modifier Interop type embedding (“No PIA”) Automatic object  dynamic mapping

17 Improved COM Interoperability
Tech Ed North America 2010 9/13/2018 1:09 PM Improved COM Interoperability DEMO © 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.

18 Designing Dynamic Initial sentiment
C# is not a dynamic language! Dynamic is a dangerous foreign element It should be syntactically explicit

19 Static Example string[] strings = GetStrings();
string last = strings[strings.Length – 1];

20 Explicit Dynamic Operations
object strings = GetDynamicObject(); string last = ~(string)strings~[strings~.Length ~– 1]; object strings = GetDynamicObject(); string last = (string)strings~[strings~.Length ~– 1]; object strings = GetDynamicObject(); string last = strings~[strings~.Length ~– 1]; object strings = GetDynamicObject(); string last = strings[strings~.Length – 1]; object strings = GetDynamicObject(); string last = strings~[strings~.Length – 1]; object strings = GetDynamicObject(); string last = strings[strings.Length – 1];

21 Explicit Dynamic Operations
object strings = GetDynamicObject(); string last = ~(string)strings~[strings~.Length ~– 1]; Reads horribly! Insight: Dynamic operations travel in packs!

22 Dynamic Blocks object strings = GetDynamicObject(); string last;
dynamic { last = strings[strings.Length – 1]; }

23  Dynamic Blocks Different “dialect” of C# inside
object strings = GetDynamicObject(); string last; dynamic { last = strings[strings.Length – 1]; } Different “dialect” of C# inside Opt out with static contexts? Lose sight of big contexts Insight: Dynamicness flows from within

24 Contagious Dynamic Expressions
object strings = GetDynamicObject(); string last = strings[dynamic(strings).Length – 1];

25 Contagious Dynamic Expressions
object strings = GetDynamicObject(); string last = strings[dynamic(strings).Length – 1]; Rules of propagation – which operations are dynamic? Factoring out subexpressions is hard – what is their type? Insight: Dynamicness follows the objects

26 The dynamic Type dynamic strings = GetDynamicObject();
string last = strings[strings.Length – 1];

27  The dynamic Type Propagates through the types Reads like normal C#!
dynamic strings = GetDynamicObject(); string last = strings[strings.Length – 1]; Propagates through the types Reads like normal C#!

28 Why is this OK? What Happened to Syntactically Explicit?
Embrace dynamicness You can’t make a language feature that is “built to be ugly” Dynamic isn’t there to take away safety But to make ugly code nice Dynamicness is still explicit – just not in syntax What better way to express lack of typing than through types?

29   Type or Type Modifier? Generality: Simplicity:
Static binding of Foo’s members Dynamic binding of the rest Simplicity: Dynamic binding of all members Even those on Object dynamic Foo foo = GetDynamicFoo(); dynamic foo = GetDynamicFoo();

30   Dynamic Binding When? When the receiver is dynamic:
Forces you to choose a type When any subexpression is dynamic: Softer “landing” back in type land dynamic result = Math.Abs((double)d); dynamic result = Math.Abs(d);

31 Dynamic Operations Dynamic result: Static result:
Method call Math.Abs(d) Invocation d(“Hello”) Member access d.Length Operator application 4 + d Indexing d[“Hello”] Static result: Conversions (double)d Object creation new Foo(d)

32 How Dynamic? “Just enough” dynamicness Principle of least surprise:
M(d, GetFoo()); Bind with runtime type Bind with compile-time type “Just enough” dynamicness Nobody uses your runtime type unless you let them Principle of least surprise: Argument’s contribution to binding is invariant

33 Summary: The Meaning of dynamic
dynamic means “use my runtime type for binding”

34 Summary: The Reason for dynamic Which would you rather see – or write?
Type calcType = calc.GetType(); object res = calcType.InvokeMember("Add", BindingFlags.InvokeMethod, null, new object[] { 10, 20 }); int sum = Convert.ToInt32(res); int sum = calc.Add(10, 20);

35 Takeaways C# 4.0 is about interacting with a bigger world
Dynamic allows anything to “look like an object” Named and optional eliminate overloads and boilerplate COM interop is finally natural in C#

36 Related Content – Breakout Sessions
Required Slide Speakers, please list the Breakout Sessions, Interactive Sessions, Labs and Demo Stations that are related to your session. Tech Ed North America 2010 9/13/2018 1:09 PM Related Content – Breakout Sessions DEV307: F# in Microsoft Visual Studio 2010 (6/10 from 9:45am – 11:00am) DEV315: Microsoft Visual Studio 2010 Tips and Tricks (6/8 from 5:00pm – 6:15pm) DEV316: Modern Programming with C++Ox in Microsoft Visual C (6/8 from 3:15pm – 4:30pm) DEV319: Scale and Productivity for C++ Developers with Microsoft Visual Studio 2010 (6/9 from 8:00am – 9:15am) DEV401: Advanced Use of the new Microsoft Visual Basic 2010 Language Features (6/9 from 9:45am – 11:00am) DEV404: C# in the Big World (6/8 from 1:30pm – 2:45pm) DEV406: Integrating Dynamic Languages into Your Enterprise Applications (6/8 from 8am – 9:15am) DEV407: Maintaining and Modernizing Existing Applications with Microsoft Visual Studio 2010 (6/10 from 8:00am – 9:15am) © 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.

37 Related Content – Interactive Sessions and HOL's
Required Slide Speakers, please list the Breakout Sessions, Interactive Sessions, Labs and Demo Stations that are related to your session. Tech Ed North America 2010 9/13/2018 1:09 PM Related Content – Interactive Sessions and HOL's DEV03-INT: Meet the C# team (6/9 from 1:30-2:45pm) DEV04-INT: Meet the VB team (6/10 from 3:15 – 4:30pm) DEV09-INT: Microsoft Visual Basic and C# IDE Tips and Tricks (6/7 from 4:30pm -5:45pm) DEV10-INT: Using Dynamic Languages to build Scriptable Applications ((6/9 from 8:00am -9:15am) DEV11 –INT: IronPython Tools (6/10 from 5:00pm – 6:15pm) DEV05-HOL: Introduction to F# © 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.

38 Required Slide Track PMs will supply the content for this slide, which will be inserted during the final scrub. Tech Ed North America 2010 9/13/2018 1:09 PM Track Resources Visual Studio – Soma’s Blog – MSDN Data Developer Center – ADO.NET Team Blog – WCF Data Services Team Blog – EF Design Blog – © 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.

39 Resources Learning Required Slide www.microsoft.com/teched
Tech Ed North America 2010 9/13/2018 1:09 PM Required Slide Resources Learning Sessions On-Demand & Community Microsoft Certification & Training Resources Resources for IT Professionals Resources for Developers © 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.

40 Complete an evaluation on CommNet and enter to win!
Tech Ed North America 2010 9/13/2018 1:09 PM Required Slide Complete an evaluation on CommNet and enter to win! © 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.

41 Sign up for Tech·Ed 2011 and save $500 starting June 8 – June 31st
You can also register at the North America 2011 kiosk located at registration Join us in Atlanta next year

42 Tech Ed North America 2010 9/13/2018 1:09 PM
© 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. © 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.

43 Required Slide Tech Ed North America 2010 9/13/2018 1:09 PM
© 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.


Download ppt "C# in the Big World Mads Torgersen Program Manager"

Similar presentations


Ads by Google