Skip to content

Instantly share code, notes, and snippets.

Avatar

Christian Nagel christiannagel

View GitHub Profile
@christiannagel
christiannagel / UseClientHandlerSample.cs
Created Jun 8, 2018
Add a HttpClientHandler to a HTTP Client Factory configuration
View UseClientHandlerSample.cs
private ServiceProvider ConfigureServices()
{
var services = new ServiceCollection();
services.AddLogging(builder =>
{
builder.AddFilter((category, level) => true);
builder.AddConsole(options => options.IncludeScopes = true);
});
services.AddHttpClient("cni", client =>
{
@christiannagel
christiannagel / Program.cs
Created Jun 19, 2018
C# 8 switch expression
View Program.cs
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"
};
@christiannagel
christiannagel / Books.cshtml
Created Jul 3, 2018
Blazor Books component accessing an Azure Function returning Book objects
View Books.cshtml
@page "/books"
@using BooksLib
@inject HttpClient Http
<h1>Books Sample</h1>
<p>This component demonstrates fetching data from Azure Functions.</p>
<p>Status: @message</p>
@christiannagel
christiannagel / BooksFunction.cs
Created Jul 2, 2018
Azure Function returning a list of Book objects
View BooksFunction.cs
public static class BooksFunction
{
[FunctionName("BooksFunction")]
public static IActionResult Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]HttpRequest req, TraceWriter log)
{
log.Info("C# HTTP trigger function processed a request.");
return new OkObjectResult(GetBooks());
}
@christiannagel
christiannagel / Book.cs
Created Jul 2, 2018
Book class in a shared .NET Standard Library
View Book.cs
public class Book
{
public Book() { }
public Book(int bookId, string title, string publisher)
=> (BookId, Title, Publisher) = (bookId, title, publisher);
public int BookId { get; set; }
public string Title { get; set; }
public string Publisher { get; set; }
@christiannagel
christiannagel / Program.cs
Last active Jun 19, 2018
C# 8: extended pattern matching with the switch expression - property pattern, recursive pattern
View Program.cs
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"
};
@christiannagel
christiannagel / Program.cs
Created Jun 19, 2018
Sample Main method for pattern matching
View Program.cs
static void Main()
{
var r1 = new Rectangle(position: (200, 200), size: (200, 200));
var e1 = new Ellipse(position: (80, 1400), size: (80, 140));
var shapes = new Shape[]
{
r1,
e1,
new Circle((40, 60), 90),
new CombinedShape(r1, e1)
@christiannagel
christiannagel / Shape.cs
Created Jun 19, 2018
Shape class used by pattern matching samples
View Shape.cs
public abstract class Shape
{
public (int x, int y) Position { get; }
public (int height, int width) Size { get; }
public Shape((int x, int y) position, (int height, int width) size)
=> (Position, Size) = (position, size);
public void Deconstruct(out (int x, int y) position, out (int x, int y) size)
=> (position, size) = (Position, Size);
@christiannagel
christiannagel / Program.cs
Created Jun 19, 2018
C# 7 pattern matching with the switch statement and type patterns with a filter
View Program.cs
static string M1(Shape shape)
{
switch (shape)
{
case Shape s when s.Size.height > 100:
return $"large shape with size {s.Size} at position {s.Position}";
case Ellipse e:
return $"Ellipse with size {e.Size} at position {e.Position}";
case Rectangle r:
return $"Rectangle with size {r.Size} at position {r.Position}";
@christiannagel
christiannagel / Program.cs
Created Jun 15, 2018
Using a C# 8 library from a C# 7 application
View Program.cs
var glory = new NewAndGlory();
string s1 = glory.GetANullString();
string s2 = glory.GetAString();
string s3 = glory.PassAString(null); // having a NullReferenceException here!
You can’t perform that action at this time.