Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Reference Semantics with C# and .NET Core

257 views

Published on

Recent changes with C# 7 returning values by references, ref locals and ref returns, readonly struct, ref struct, in modifier, the Span<T> type and how it is used with libraries - performance improvements on the string type, stream overloads with Span parameters, and the System.IO.Pipeline library

Published in: Software
  • Be the first to comment

  • Be the first to like this

Reference Semantics with C# and .NET Core

  1. 1. Referenz Semantik mit C# und .NET Core @christiannagel
  2. 2. Referenz Semantik mit C# Demo
  3. 3. Ref / Value • class • Pass by reference • Stored on the heap Reference Types • struct • Pass by value • Stored on the stack – or the heap • Boxing! Value Types
  4. 4. Agenda Traditional Ref New Syntax Features Use in Libraries
  5. 5. Christian Nagel • Training • Coaching • Consulting • Development • Microsoft MVP • www.cninnovation.com • csharp.christiannagel.com
  6. 6. Struct .NET Design Guidelines • Do not provide a default constructor • Not allowed by C# • Do not define mutable value types • Developers might not be aware to mutate a copy • Do ensure that zero/false/null state is valid • Prevents accidential creation of invalid instances • Do implement IEquatable<T> • Object.Equals causes boxing • Do not explicitly extend value types • Not allowed by C#
  7. 7. Rule: "Do not define mutable value types"! Ausnahmen! short/int/long/ float/double Tuples
  8. 8. readonly struct • C# 7.2 readonly struct MyStruct { public MyStruct(int myProperty) => MyProperty = myProperty; public int MyProperty { get; } }
  9. 9. Demo • ref local • ref return • readonly ref • in
  10. 10. ref return / ref local • Eine Referenz auf einen Value Type zurück liefern • ref local public class Container { public Container(int[] data) => _data = data; private int[] _data; public ref int GetItem(int index) => ref _data[index]; } ref int item = ref c.GetItem(3);
  11. 11. ref readonly • Eine Referenz, aber nicht änderbar public ref readonly int GetItem(int index) => ref _data[index]; ref readonly int item = ref c.GetItem(3); // item = 4; // error – cannot be changed!
  12. 12. in Parameter Modifier • Pass by ref – aber nicht änderbar void PassByReference(in int x) { // cannot change x }
  13. 13. Demo • ref struct • Span<T> • Slices • Span using arrays and strings
  14. 14. The Span<T> Type stack only Verwendet ref return Zugriff auf kontinuierliches Memory Stack, Managed Heap, Native Heap
  15. 15. Libraries
  16. 16. String Performance (.NET Core 2.1) • String.Equal • String.IndexOf, String.LastIndexOf • String.ToLower • String.Split • String.Format • StringBuilder.Append • int.Parse • Algorithms from vectorized computing (like used with Span<T>) • Using Span<T> internally • Reducing allocations • Fast paths for common scenarios
  17. 17. ArrayPool Werden viele Arrays dynamisch erzeugt? ArrayPool verwaltet Arrays Rent Arrays vom Pool Return Arrays zum Pool Thread safe! .NET Core 2.1 MemoryPool für Buffer
  18. 18. Span<T> Overloads Neue Overloads für Stream Operations Vereinfacht Code – Größenangaben entfallen Performanceverbesserung
  19. 19. Pipelines • Management von Buffers • Wird von Kestrel und SignalR verwendet • System.IO.Pipelines • Pipe • allocates and manages memory • PipeReader / PipeWriter • read and write side of the pipe • ReadOnlySequence • linked list of buffers
  20. 20. Summary C# Keywords für Referenz Semantics Performance verbessert Memory Allocations reduziert Libraries vereinfacht
  21. 21. Questions?
  22. 22. More Information • https://github.com/ProfessionalCSharp • https://csharp.christiannagel.com • https://www.cninnovation.com • Training & Coaching
  23. 23. Thank you!

×