1 / 46

10 dingen die je niet wist over C#

10 dingen die je niet wist over C#. C#. Lezen. ALM. ALM Ranger. Microsoft. Getrouwd. Auteur. Junior. Medior. Senior. private static string A( string a, string b, string c) { string result = "{a:" + a + ", b:" + b + ", c:" + c + "}" ; return result ; }.

hakeem-knox
Download Presentation

10 dingen die je niet wist over C#

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. 10 dingen die je niet wist over C#

  2. C# Lezen ALM ALM Ranger Microsoft Getrouwd Auteur

  3. Junior

  4. Medior

  5. Senior

  6. privatestaticstring A(string a, string b, string c) { string result = "{a:" + a + ", b:" + b + ", c:" + c + "}"; returnresult; } privatestaticstring B(string a, string b, string c) { StringBuildersb = newStringBuilder(100); string result = sb.Append("{a:").Append(a) .Append(", b:").Append(b) .Append(", c:").Append(c) .Append("}") .ToString(); returnresult; } B A A&B

  7. staticvoidMain() { CapitalLetters(null); } staticIEnumerable<char> CapitalLetters(string input) { if (input == null) { thrownewArgumentNullException(input); } foreach (char c in input) { yieldreturnchar.ToUpper(c); } } Nee Ja

  8. var result = fromiinnewList<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } select i; foreach (var r inresult) Console.Write(r); // Displays 02468 Compiler bug Extension methods IL rewriting

  9. var a = "dev"; var b = " Net "; var c = "Noord"; Console.WriteLine(a + b + c); // Displays devNetNoord Wat is var hier? class keyword

  10. publicclassHomeController : Controller { publicasyncTask<ViewResult>Index() { var pi = awaitTask.Run<double>(Helper.CalculatePi(1000)); return View(pi); } } asynchroon synchroon

  11. privateasyncstaticTask Run() { await A(); // A takes 1 second await B(); // B takes 2 seconds } 1 2 3

  12. staticList<Order> cache; void Run() { if (cache == null) { cache = FetchItems(); } // workwith cache } 1 Ja Nee

  13. staticvoidMain() { int n = 0; var up = Task.Run(() => { for (int i = 0; i < 1000000; i++) n++; }); for (int i = 0; i < 1000000; i++) n--; up.Wait(); Console.WriteLine(n); } 0 < 0 > 0

  14. staticvoid Foo<T>() where T : new() { T t = new T(); Console.WriteLine("ToString: " + t.ToString()); Console.WriteLine("GetHashCode: " + t.GetHashCode()); Console.WriteLine("Equals: " + t.Equals(t)); Console.WriteLine("GetType: " + t.GetType()); } Foo<int> Foo<int?> Foo<string>

  15. staticvoid Foo<T>() where T : new() { A: T t = new T(); B: Console.WriteLine("ToString: " + t.ToString()); C: Console.WriteLine("GetHashCode: " + t.GetHashCode()); D: Console.WriteLine("Equals: " + t.Equals(t)); E: Console.WriteLine("GetType: " + t.GetType()); } D C E B A

  16. classBase { publicvirtualvoidFoo(int x) { Console.WriteLine("Base.Foo(int)"); } } classDerived : Base { publicoverridevoidFoo(int x) { Console.WriteLine("Derived.Foo(int)"); } publicvoidFoo(object o) { Console.WriteLine("Derived.Foo(object)"); } } -2147483639 newDerived().Foo(10);

  17. int i1 = 2147483647 + 10; Ja Nee

  18. intten = 10; int i2 = 2147483647 + ten; Ja Nee

  19. intten = 10; int i2 = 2147483647 + ten; Wat is de uitkomst?

  20. Tot zover de quiz

  21. Any fool can know The point is to understand

  22. Multithreading Performance Syntactic sugar

  23. Multithreading

  24. Async en await

  25. Async? privateasyncstaticTask<int> RunCPU() { string content = awaitTask.Run<string>(() => "Hello devNetNoord!"); returncontent.Length; } privateasyncstaticTask<int> RunIO() { HttpClientclient = newHttpClient(); stringresult = awaitclient.GetStringAsync("http://www.devnetnoord.nl"); returnresult.Length; }

  26. Platform ThreadPool UIThread • Client • Server • CPU • I/O • CPU • I/O

  27. Async is niet parallel privateasyncstaticTask Run() { await A(); // A takes 1 second await B(); // B takes 2 seconds } privateasyncstaticTaskRunParallel() { Task a = A(); // A takes 1 second Task b = B(); // B takes 2 seconds awaitTask.WhenAll(a, b); }

  28. Performance

  29. Generation 2 objects Allocated Generation 1 objects Generation 0 objects Commited Uncommited Free

  30. Boxing & unboxing

  31. classUnmanagedWrapper : IDisposable { privateIntPtrunmanagedBuffer; publicFileStream Stream { get; privateset; } publicUnmanagedWrapper() { CreateBuffer(); this.Stream = File.Open("temp.dat", FileMode.Create); } privatevoidCreateBuffer() { … } ~UnmanagedWrapper() { Dispose(false); } publicvoid Close() { Dispose(); } publicvoidDispose() { Dispose(true); System.GC.SuppressFinalize(this); } protectedvirtualvoid Dispose(bool disposing) { Marshal.FreeHGlobal(unmanagedBuffer); if (disposing) { if (Stream != null) { Stream.Close(); } } } }

  32. using (UnmanagedWrapper u = newUnmanagedWrapper()) { }

  33. Syntacticsugar

  34. C# • IL • Machine code init ([0] class [mscorlib]System.Collections.Generic.List`1<int32> numbers, [1] int32 n, [2] class [mscorlib]System.Collections.Generic.List`1<int32> '<>g__initLocal0', [3] valuetype var numbers = new List<int> {  1, 2, 3, 4 }; mov ecx,79848540h call FFD2FAC0 movdwordptr [ebp-5Ch],eax movecx,dwordptr [ebp-5Ch] call 78BFBF90 moveax,dwordptr [ebp-5Ch] movdwordptr [ebp-44h],eax movecx,dwordptr [ebp-44h] mov edx,1 cmpdwordptr [ecx],ecx call 78BE24C0

  35. yield async & await foreach using lock Nullable<T> …

  36. ildasm

  37. En nu? John Skeet: C# in Depth Jeffrey Richter: CLR via C# Eric Lippert: http://ericlippert.com/ Stackoverflow

  38. @wouterdekort wouter.de.kort@ordina.nl http://wouterdekort.blogger.com

More Related