1 / 18

C# 4.0

Alexandru Ghiondea C# Compiler SDET. C# 4.0. The Evolution of C#. C# 4.0. Dynamic Programming. C# 3.0. Language Integrated Query. C# 2.0. Generics. C# 1.0. Managed Code. C# 4.0 Language Innovations. Dynamically Typed Objects Optional and Named Parameters

yovela
Download Presentation

C# 4.0

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Alexandru Ghiondea C# Compiler SDET C# 4.0

  2. The Evolution of C# C# 4.0 Dynamic Programming C# 3.0 Language Integrated Query C# 2.0 Generics C# 1.0 Managed Code

  3. C# 4.0 Language Innovations • Dynamically Typed Objects • Optional and Named Parameters • Improved COM Interoperability

  4. .NET Dynamic Programming IronPython IronRuby C# VB.NET Others… Dynamic Language Runtime Expression Trees Dynamic Dispatch Call Site Caching ObjectBinder JavaScriptBinder PythonBinder RubyBinder COMBinder

  5. Dynamically Typed Objects Calculator calc = GetCalculator(); int sum = calc.Add(10, 20); .NET object object calc = GetCalculator(); TypecalcType = calc.GetType(); object res = calcType.InvokeMember("Add", BindingFlags.InvokeMethod, null, newobject[] { 10, 20 }); int sum = Convert.ToInt32(res); Dynamic Language object 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

  6. Under the cover • dynamic is a new type only in the compiler • The return type of a dynamic operation is dynamic • There is an assignment conversion from dynamic to any type • We support all types of operations on dynamic

  7. Dynamically Typed Objects DEMO

  8. IDynamicObject publicabstractclassDynamicObject : IDynamicObject { publicvirtualobjectGetMember(GetMemberBinder info); publicvirtualobjectSetMember(SetMemberBinder info, object value); publicvirtualobjectDeleteMember(DeleteMemberBinder info); publicvirtualobjectUnaryOperation(UnaryOperationBinder info); publicvirtualobjectBinaryOperation(BinaryOperationBinder info, objectarg); publicvirtualobject Convert(ConvertBinder info); publicvirtualobject Invoke(InvokeBinder info, object[] args); publicvirtualobjectInvokeMember(InvokeMemberBinder info, object[] args); publicvirtualobjectCreateInstance(CreateInstanceBinder info, object[] args); publicvirtualobjectGetIndex(GetIndexBinder info, object[] indices); publicvirtualobjectSetIndex(SetIndexBinder info, object[] indices, object value); publicvirtualobjectDeleteIndex(DeleteIndexBinder info, object[] indices); publicMetaObjectIDynamicObject.GetMetaObject(); }

  9. Implementing IDynamicObject DEMO

  10. Improved COM Interoperability • Automatic object  dynamic mapping • Optional and named parameters • Optional “ref” modifier • Interop type embedding (“No PIA”)

  11. object -> dynamic mapping We need to cast ((Excel.Range)xl.Cells[1,1]).Value2 = “ID”; xl.Cells[1,1].Value2 = “ID”; You can continue to “dot”

  12. Optional and named parameters xlWorkbook.Worksheets.Add(Type.Missing, xlWorkbook.ActiveSheet, Type.Missing, Type.Missing); Arguments evaluated in order written xlWorkbook.Worksheets.Add(After:xlWorkbook.ActiveSheet); Named arguments can appear in any order

  13. Optional and named parameters Optional parameters publicStreamReaderOpenTextFile( string path, Encodingencoding, booldetectEncoding, intbufferSize); publicStreamReaderOpenTextFile( string path, Encodingencoding = null, booldetectEncoding = true, intbufferSize = 1024); Non-optional must be specified OpenTextFile("foo.txt”); OpenTextFile("foo.txt", Encoding.UTF8, bufferSize: 4096); Named argument

  14. Optional “ref” modifier objectfileName = "Test.docx"; object missing = System.Reflection.Missing.Value; doc.SaveAs(reffileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); doc.SaveAs("Test.docx");

  15. Under the cover • The default value for a parameter is encoded with a DefaultParameterValue attribute • No change in overload resolution • The “omit ref” only works for COM objects • No-PIA encapsulates the used interfaces into the assembly

  16. Improved COM Interoperability DEMO

  17. Additional Resources • C# 4.0 Samples and Whitepaper • http://code.msdn.microsoft.com/csharpfuture • Visual C# Developer Center • http://csharp.net • C# team member’s blogs • http://blogs.msdn.com/ericlippert/ • http://blogs.msdn.com/cburrows/ • http://blogs.msdn.com/samng/ • http://blogs.msdn.com/sreekarc/ • http://blogs.msdn.com/mattwar/ • http://blogs.msdn.com/ed_maurer/ • http://blogs.msdn.com/davsterl/ • http://blogs.msdn.com/alexghi

  18. Q&A

More Related