CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
C# 8 in Libraries and Applications - BASTA! Frankfurt 2020
1. C# 8 in Libraries and
Applications
Christian Nagel
https://csharp.christiannagel.com
2. Topics
New C# 8 Features
Moving from existing code
Guidelines
3. Christian Nagel
• Training
• Coaching
• Consulting
• Development
• Microsoft MVP
• www.cninnovation.com
• csharp.christiannagel.com
• github.com/cninnovation
5. How to use
C# 8
• By default enabled with .NET Core 3.0 and .NET Standard 2.1
• Enable with other projects: <LangVersion>
• Directory.Build.Props File
6. Features based on
Frameworks/Runtimes
• Syntax sugar features with all frameworks
• Some features need specific classes &
interfaces
• Ranges, async streams
• Runtime updates required
• .NET Standard 2.1
• Default Interface Members
14. Guidelines
• Library authors – Nullable adoption phase up to
.NET 5
• App developers – nullability on your own pace
• Annotate new APIs
• Do not remove argument validation
• Parameter is non-nullable if parameters are
checked (ArgumentNullException)
• Parameter is nullable if documented to accept
null
• Prefer nullable over non-nullable with
disagreements
17. Default Interface Methods
Changing of
interfaces without
breaking changes
1
Traits – Reuse
implementations in
independent types
2
Based on Java's
Default Methods
3
18. Default Interface Methods
• Changing the interface without breaking changes
public interface ILogger
{
void Log(string message);
}
public interface Ilogger
{
void Log(string message);
void Log(Exception ex) => Log(ex.Message);
}
public class MyLogger : ILogger
{
public void Log(string message) => Console.WriteLine(message);
}
19. Tipps
Is the first version of the interface
complete?
Additions are now possible!
Alternative to extension methods
Runtime extensions needed!
23. Range
string text1 = "the quick brown fox jumped over the lazy dogs";
string quick = text1[4..9];
string dog = text1[^4..^1];
string brownfoxjumpedandmore = text1[10..];
string thequick = text1[..8];
string text2 = text1[..];
24. Demo
• Ranges and Indices
• Access Slice of Span<T>
• Access String
• Range, Index Types
• Custom Collections
27. Async
Streams
Async before: async/await returns one
result
Async streams extends async/await with
a stream of results
Async sources controlled by the
consumer
Alternative to System.Reactive
28. Async Streams
• IAsyncDisposable
• IAsyncEnumerable
• IAsyncEnumerator
public interface IAsyncDisposable
{
ValueTask DisposeAsync();
}
public interface IAsyncEnumerable<out T>
{
IAsyncEnumerator<T> GetAsyncEnumerator();
}
public interface IAsyncEnumerator<out T> :
IAsyncDisposable
{
ValueTask<bool> MoveNextAsync();
T Current { get; }
}
29. Using Async Streams
• await foreach
IAsyncEnumerator<T> enumerator =
enumerable.GetAsyncEnumerator();
try
{
while (await enumerator.MoveNextAsync())
{
Use(enumerator.Current);
}
}
finally
{
await enumerator.DisposeAsync();
}
await foreach (var i in enumerable)
{
Use(i);
}
30. Async with yield
• return IAsyncEnumerable static async IAsyncEnumerable<int> MyIterator()
{
try
{
for (int i = 0; i < 100; i++)
{
await Task.Delay(1000);
yield return i;
}
}
finally
{
await Task.Delay(200);
Console.WriteLine("finally");
}
}
31. Demo
• Async Streams & SignalR
• Streaming from the Server to the
Client
• Streaming from the Client to the
Server
• C# 8 Async Streams
33. using declaration
public void Method(IEnumerable<string> lines)
{
using var file = new StreamWriter("sample.txt");
foreach (var line in lines)
{
file.WriteLine(line);
}
}
34. switch Expression
static string M2(Shape shape) =>
shape switch
{
Shape s when s.Size.height > 100 =>
$"large shape with size {s.Size} at position {s.Position}",
Ellipse e =>
$"Ellipse with size {e.Size} at position {e.Position}",
Rectangle r =>
$"Rectangle with size {r.Size} at position {r.Position}",
_ => "another shape"
}
};
35. Recursive, Property, and Discard Patterns
static string M3(Shape shape) =>
shape switch
{
CombinedShape (var shape1, var (pos, _)) =>
$"combined shape - shape1: {shape1.Name}, pos of shape2: {pos}",
{ Size: (200, 200), Position: var pos } =>
$"shape with size 200x200 at position {pos.x}:{pos.y}",
Ellipse (var pos, var size) =>
$"Ellipse with size {size} at position {pos}",
Rectangle (_, var size) => $"Rectangle with size {size}",
_ => "another shape"
};
39. For action
Annotate libraries with nullability
Use new C# 8 features
https://csharp.christiannagel.com
https://github.com/christiannagel
https://github.com/ProfessionalCSharp