Created
December 21, 2020 02:18
-
-
Save MelbourneDeveloper/a8877827d366204831762a1ca8540e5c to your computer and use it in GitHub Desktop.
.NET Standard Sample Code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using System.Text.Json; | |
using System.Threading.Tasks; | |
namespace DotNetStandardLibrary | |
{ | |
public class ExampleClass | |
{ | |
public string Test { get; } | |
public ExampleClass(string test) | |
{ | |
Test = test; | |
} | |
/// <summary> | |
/// Uses the Span type | |
/// </summary> | |
public ExampleClass WriteSpans() | |
{ | |
var spans = new Span<string>(new string[] { "One", "Two" }); | |
foreach (var span in spans) | |
{ | |
Console.WriteLine(span); | |
} | |
return this; | |
} | |
/// <summary> | |
/// Use an Async foreach with IAsyncEnumerable | |
/// </summary> | |
public static async Task DoAsyncNumbersAsync() | |
{ | |
var asyncEnumerable = AsyncEnumerable.Range(0, 10); | |
await foreach (var number in asyncEnumerable) | |
{ | |
Console.WriteLine($"Awaited Number: {number}"); | |
} | |
} | |
/// <summary> | |
/// Serialize and Deserialize with System.Text.Json | |
/// </summary> | |
public ExampleClass DoSerialize() | |
{ | |
var dailyTemperature = new DailyTemperature(10, 20); | |
var json = JsonSerializer.Serialize(dailyTemperature); | |
dailyTemperature = JsonSerializer.Deserialize<DailyTemperature>(json); | |
if (dailyTemperature == null) | |
{ | |
throw new InvalidOperationException(); | |
} | |
Console.WriteLine($"Json: {json}\r\nHigh: {dailyTemperature.HighTemp} Low: {dailyTemperature.LowTemp}"); | |
return this; | |
} | |
} | |
public static class Extensions | |
{ | |
/// <summary> | |
/// C# Pattern matching example | |
/// </summary> | |
public static bool IsLetter(this char c) => c is (>= 'a' and <= 'z') or (>= 'A' and <= 'Z'); | |
} | |
/// <summary> | |
/// IAsyncDisposable Example | |
/// </summary> | |
public class AsyncDisposable : IAsyncDisposable | |
{ | |
public ValueTask DisposeAsync() => new ValueTask(Task.FromResult(true)); | |
} | |
/// <summary> | |
/// Record example | |
/// </summary> | |
public record DailyTemperature(double HighTemp, double LowTemp); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment