C# 4.0

1,268 views

Published on

Børge Hansen presenterte C# 4.0 og .Net 4.0 på høstens runde av MSDN & TechNet Live.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,268
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
47
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

C# 4.0

  1. 1. C# 4.0 og .Net 4.0 Børge Hansen borge@devpartner.no http://borge3000.no
  2. 2. Functional Concise Declarative
  3. 3. LINQ, The Power of Declarative IList<Person> FindParentsWithChildNamed(string childName) { var matches = new List<Person>(); foreach(var person in _people) { foreach(var child in person.Children) { Before if (child.Name.Equals(childName)) { matches.Add(person); break; } } } return matches; }
  4. 4. LINQ, The Power of Declarative IList<Person> FindParentsWithChildNamed(string childName) { var matches = from person in people from child in person.Children After where child.Name.Equals(childName) select person; return matches.ToList(); }
  5. 5. Why Declarative Matters… What How Imperative Declarative
  6. 6. Addressing Language Trends Declarative Dynamic Concurrent
  7. 7. The Evolution of C# C# 4.0 Dynamic C# 3.0 LINQ C# 2.0 Generics C# 1.0 Managed Code
  8. 8. New C# 4.0 Features 1. Late-Binding Support / dynamic lookup 2. Named and Optional Parameters 3. Improved COM Interop 4. Covariance and Contravariance
  9. 9. Dynamically Typed Objects Compile-time type Run-time type dynamic 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
  10. 10. DEMO C# 4.0 features: Dynamic keyword
  11. 11. 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); Call primary with default values public StreamReader OpenTextFile( string path, Encoding encoding); public StreamReader OpenTextFile( string path);
  12. 12. Optional and Named Parameters public StreamReader OpenTextFile( Optional parameters string path, Encoding encoding,= null, encoding bool detectEncoding,= true, detectEncoding int bufferSize); 1024); bufferSize = Named argument OpenTextFile("foo.txt", Encoding.UTF8); OpenTextFile("foo.txt", Encoding.UTF8, bufferSize: 4096); Named arguments can Arguments evaluated Named arguments appear in any order in order written must be last OpenTextFile( Non-optional must be bufferSize: 4096, path: "foo.txt", specified detectEncoding: false);
  13. 13. DEMO C# 4.0 features: Named and optional parameters
  14. 14. 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, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); doc.SaveAs("Test.docx");
  15. 15. Improved COM Interoperability • Automatic object  dynamic mapping • Optional and named parameters • Indexed properties • Optional “ref” modifier • Interop type embedding (“No PIA”)
  16. 16. DEMO C# 4.0 features: Com Interop
  17. 17. How Do You Know What It Does? string s = o.GetDescription(i); … s.Length … /// <param name=“x”> /// should be greater than zero /// </param> /// <returns> /// non-null string /// </returns> string GetDescription(int x);
  18. 18. Let’s Fix It For Once And All! Design-by-Contract meets .NET! string GetDescription(int x) { Contract.Requires( 0 < x ); Contract.Ensures( Contract.Result<string>() != null ); … }
  19. 19. Contracts • Contracts document design decisions – Preconditions, postconditions, invariants – Facilitate code evolution • Contracts are executable – Checked documentation – Test oracles • Contracts help static verification – Early error detection – Clarify responsibility at API boundaries • Assertions help, but everyone has their own flavor – Difficult to tool against – Not seen as a contract between 2 parties
  20. 20. What Can I Do With Contracts? class Rational { public Rational(int n, int d) { Contract.Requires( 0 < d ); this.N = n; this.D = d; } } Static Checking Runtime Test Checking Generation (Pex) Documentation
  21. 21. What Contracts Can I Write? • Requires – What must be true at method entry • Ensures – What must be true at method exit • Invariants – What must be true at all method exits • Assertions – What must be true at a particular point • Assumptions – What should be true at a particular point
  22. 22. What Can I Put In A Contract? • Any boolean expression – In your favorite programming language! – Including method calls (but must be marked Pure) • Quantifiers – Contract.ForAll(0,A.Length,i => A[i] > 0); – Contract.Exists(0,A.Length,i => A[i] > 0); • Contract.Result – refer to the return value of the method • Contract.OldValue – refer to values at method entry
  23. 23. DEMO C# 4.0 features: Code contracts
  24. 24. Additional Resources • C# 4.0 Samples and Whitepaper http://code.msdn.microsoft.com/csharpfuture • Visual C# Developer Center http://csharp.net • Visual Studio 2010 Training kit http://www.microsoft.com/downloads/details.aspx?FamilyID=752cb725- 969b-4732-a383-ed5740f02e93&DisplayLang=en • Code contracts http://research.microsoft.com/en- us/projects/contracts
  25. 25. Børge Hansen borge@devpartner.no http://borge3000.no +47 905 905 38

×