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.

Successfully reported this slideshow.

Like this presentation? Why not share!

- MVP Showcase 2015 - C# by Paulo Morgado 903 views
- What's new in C# 6 - NetPonto Port... by Paulo Morgado 1201 views
- await Xamarin @ PTXug by Paulo Morgado 756 views
- Roslyn analyzers: File->New->Project by Paulo Morgado 728 views
- As Novidades Do C# 4.0 - NetPonto by Paulo Morgado 3000 views
- Async-await best practices in 10 mi... by Paulo Morgado 4236 views

3,363 views

Published on

Published in:
Technology

No Downloads

Total views

3,363

On SlideShare

0

From Embeds

0

Number of Embeds

29

Shares

0

Downloads

113

Comments

0

Likes

1

No embeds

No notes for slide

- 1. What's New In C# 4.0<br />
- 2. Paulo Morgado<br />
- 3. The Evolution Of C#<br />Covariance And Contravariance<br />Named And Optional Arguments<br />Dynamic Programming<br />COM Interop Improvements<br />Agenda<br />
- 4. The Evolution Of C#<br />
- 5. The Evolution Of C#<br />
- 6. Trends<br />
- 7. Dynamic vs. Static<br />
- 8. VB<br /><ul><li>Named Arguments
- 9. Optional Arguments
- 10. Index Properties (COM)
- 11. Lambda Statements
- 12. Auto-Implemented Properties
- 13. Colection Initializers</li></ul>C#<br />C#<br />VB<br />Co-Evolution<br />
- 14. The Evolution Of C#<br />
- 15. The Evolution Of C#<br />http://paulomorgado.net/en/blog/archive/2010/04/12/the-evolution-of-c.aspx<br />Resources<br />
- 16. Covariance And Contravariance<br />
- 17. In multilinear algebra and tensor analysis, covariance and contravariance describe how the quantitative description of certain geometrical or physical entities changes when passing from one coordinate system to another.<br />Wikipedia<br />Covariance And Contravarance<br />
- 18. Type Tis greater than (>) type Sif Sis a subtype of (derives from) T<br />Covariance And Contravarance<br />T≥S<br />typeof(T).IsAssignableFrom(typeof(S))<br />
- 19. Give 2 types Base and Derived, such that:<br />There is a reference (or identity) conversion between Base and Derived<br />Base ≥ Derived<br />A generic type definition Generic<T> is:<br />Covariant in T<br />If Genérico<Base> ≥ Genérico<Derived><br />Contravariant in T<br />If Genérico<Base> ≤ Genérico<Derived><br />Invariant in T<br />If neither of the above apply<br />Covariance And Contravarance<br />
- 20. Contravariante em T<br />Covariante em T<br />typeof(Base).IsAssignableFrom(typeof(Derived))<br />typeof(Base).IsAssignableFrom(typeof(Derived))<br />typeof(G<Derived>).IsAssignableFrom(typeof(G<Base>))<br />typeof(G<Base>).IsAssignableFrom(typeof(G<Derived>))<br />Covariance And Contravarance<br />
- 21. Covariance And Contravarance<br />C# (.NET) arrays are covariant<br />string[] strings = GetStringArray();<br />Process(strings);<br />… but not safely covariant<br />void Process(object[] objects) { … }<br />void Process(object[] objects)<br />{<br /> objects[0] = "Hello"; // Ok<br /> objects[1] = newButton(); // Exception!<br />}<br />
- 22. Covariance And Contravarance<br />Until now, C# (.NET) generics have been invariant<br />List<string> strings = GetStringList();<br />Process(strings);<br />C# 4.0 supports safe covariance and contravariance<br />void Process(IEnumerable<object> objects)<br />{<br />// IEnumerable<T> is read-only and<br />// therefore safely covariant<br />}<br />void Process(IEnumerable<object> objects) { … }<br />
- 23. Safe Covariance<br />out= covariant<br />Output positions only<br />publicinterfaceIEnumerable<T><br />{<br />IEnumerator<T> GetEnumerator();<br />}<br />publicinterfaceIEnumerable<out T><br />{<br />IEnumerator<T> GetEnumerator();<br />}<br />Can be treated as less 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 />
- 24. Safe Contravariance<br />in= contravariantInput positions only<br />Can be treated as more derived<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 />IComparer<object> objComp = GetComparer();<br />IComparer<string> strComp = objComp;<br />
- 25. Supported for generic interface and delegate types only.<br />Verified/enforced staticaliy in the definition.<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 types<br />Variance In C# 4.o<br />
- 26. Variance In .NET 4.0<br />
- 27. Covariance And Contravariance In Generics<br />http://paulomorgado.net/en/blog/archive/2010/04/13/c-4-0-covariance-and-contravariance-in-generics.aspx<br />Covariance And Contravariance In Generics Made Easy<br />http://paulomorgado.net/en/blog/archive/2010/04/15/c-4-0-covariance-and-contravariance-in-generics-made-easy.aspx<br />Covarince and Contravariance in Generics<br />http://msdn.microsoft.com/library/dd799517(VS.100).aspx<br />Exact rules for variance validity<br />http://blogs.msdn.com/ericlippert/archive/2009/12/03/exact-rules-for-variância-validity.aspx<br />Events get a little overhaul in C# 4, Afterward: Effective Events<br />http://blogs.msdn.com/cburrows/archive/2010/03/30/events-get-a-little-overhaul-in-c-4-afterward-effective-events.aspx<br />Resources<br />
- 28. Named And Optional Arguments<br />
- 29. Named And Optional Arguments<br />Greeting("Mr.", "Morgado", 42);<br />Parameters<br />Arguments<br />public void Greeting(string title, string name, intage)<br />They always have names<br />They are never optional<br />
- 30. Named Arguments<br />int i = 0;<br />Method(i, third: i++, second: ++i);<br />int i = 0;<br />int CS$0$0000 = i++;<br />int CS$0$0001 = ++i;<br />Method(i, CS$0$0001, CS$0$0000); <br />public void Method(intfirst, intsecond, intthird)<br />
- 31. Optional Arguments<br />int i = 0;<br />Method(i, third: ++i);<br />int i = 0;<br />int CS$0$0000 = ++i;<br />Method(i, 2, CS$0$0000); <br />public void Method(intfirst, intsecond = 5, intthird = 6) <br />public void Method(intfirst, intsecond = 2, intthird = 3) <br />public void Method(intfirst, intsecond) <br />They look like overloads.<br />But they aren’t!<br />public void Method(intfirst) <br />
- 32. Argument Classes<br />Optional Arguments - Alternative<br />XmlReaderSettings settings = newXmlReaderSettings();<br />settings.ValidationType = ValidationType.Auto;<br />XmlReader.Create("file.xml", settings);<br />XmlReader.Create(<br />"file.xml",<br />new { ValidationType = ValidationType.Auto });<br />XmlReader.Create(<br />"file.xml",<br />newXmlReaderSettings { ValidationType = ValidationType.Auto });<br />Not yet!<br />
- 33. C# 4.0: Named And Optional Arguments<br />http://paulomorgado.net/en/blog/archive/2010/04/16/c-4-0-named-and-optional-arguments.aspx<br />C# 4.0: Alternative To Optional Arguments<br />http://paulomorgado.net/en/blog/archive/2010/04/18/c-4-0-alternative-to-optional-arguments.aspx<br />Named and Optional Arguments (C# Programming Guide)<br />http://msdn.microsoft.com/library/dd264739(VS.100).aspx<br />Resources<br />
- 34. Dynamic Programming<br />
- 35. Dynamic Language Runtime<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 />
- 36. Objectos Tipados Dinâmicamente<br />Calculatorcalculator = GetCalculator();<br />int sum = calc.Add(10, 20);<br />object calculator = GetCalculator();<br />Type calculatorType = calculator.GetType();<br />object res = calculatorType.InvokeMember("Add",<br />BindingFlags.InvokeMethod, null,<br /> calculator, newobject[] { 10, 20 });<br />int sum = Convert.ToInt32(res);<br />ScriptObject calculator = GetCalculator();<br />object res = calculator.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 />
- 37. 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
- 38. At run-time, actual type(s) substituted for dynamic
- 39. Static result type of operation is dynamic</li></li></ul><li>Dynamically Typed Objects<br />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 />Method chosen at compile-time:double Abs(double x)<br />double x = 1.75;<br />double y = Math.Abs(x);<br />Method chosen at<br />run-time:double Abs(double x)<br />dynamic x = 1.75;<br />dynamic y = Math.Abs(x);<br />Method chosen at<br />run-time:int Abs(int x)<br />dynamic x = 2;<br />dynamic y = Math.Abs(x);<br />
- 40. IDynamicMetaObjectProvider<br />Represents a dynamic object that can have operations determined at run-time.<br />DynamicObject : IDynamicMetaObjectProvider<br />Enables you to define which operations can be performed on dynamic objects and how to perform those operations.<br />ExpandoObject : IDynamicMetaObjectProvider<br />Enables you to add and delete members of its instances at run time and also to set and get values of these members.<br />Building Dynamic Objects<br />
- 41. Dynamic Programming<br />http://paulomorgado.net/en/blog/archive/2010/04/18/c-4-0-dynamic-programming.aspx<br />C# Proposal: Compile Time Static Checking Of Dynamic Objects<br />http://paulomorgado.net/en/blog/archive/2010/03/19/c-proposal-compile-time-static-checking-of-dynamic-objects.aspx<br />Using Type dynamic (C# Programming Guide)<br />http://msdn.microsoft.com/library/dd264736(VS.100).aspx<br />Dynamic Language Runtime Overview<br />http://msdn.microsoft.com/library/dd233052(v=VS.100).aspx<br />Resources<br />
- 42. COM Interop Improvements<br />
- 43. Named and optional arguments<br />Ommiting ref<br />Dynamic Import<br />Automatic mapping object -> dynamic<br />Indexed properties<br />Type Equivalence And Type Embedding (“NO PIA”)<br />COM Interop Improvements<br />
- 44. Ommiting ref<br />objectfileName = "Test.docx";<br />object missing = Missing.Value;<br />document.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 />document.SaveAs("Test.docx",<br />Missing.Value, Missing.Value, Missing.Value,<br />Missing.Value, Missing.Value, Missing.Value,<br />Missing.Value, Missing.Value, Missing.Value,<br />Missing.Value, Missing.Value, Missing.Value,<br />Missing.Value, Missing.Value, Missing.Value);<br />document.SaveAs("Test.docx");<br />
- 45. Dynamic Import<br />((Excel.Range)(excel.Cells[1, 1])).Value2 = "Hello World!";<br />excel.Cells[1, 1] = "Hello World!"; <br />Excel.Range range = (Excel.Range)(excel.Cells[1, 1]); <br />Excel.Rangerange = excel.Cells[1, 1]; <br />
- 46. Type Equivalence And Type Embedding (NO PIA)<br />
- 47. COM Interop Improvements<br />http://paulomorgado.net/en/blog/archive/2010/04/19/c-4-0-com-interop-improvements.aspx<br />Type Equivalence and Embedded InteropTypes<br />http://msdn.microsoft.com/library/dd997297.aspx<br />Resources<br />
- 48. Demos<br />
- 49.
- 50. Conclusion<br />
- 51. The Evolution Of C#<br />Covariance And Contravariance<br />Named And Optional Arguments<br />Dynamic Programming<br />COM Interop Improvements<br />Conclusion<br />
- 52. Visual C# Developer Center<br />http://csharp.net/<br />Visual C# 2010 Samples<br />http://code.msdn.microsoft.com/cs2010samples<br />C# Language Specification 4.0<br />http://www.microsoft.com/downloads/details.aspx?familyid=DFBF523C-F98C-4804-AFBD-459E846B268E&displaylang=en<br />Resources<br />
- 53. .NET Reflector<br />http://www.red-gate.com/products/reflector/<br />LINQPad<br />http://linqpad.net/<br />Paulo Morgado<br />http://PauloMorgado.NET/<br />Resources<br />
- 54. Thank You!<br />

No public clipboards found for this slide

Be the first to comment