Skip to content

Instantly share code, notes, and snippets.

View rstropek's full-sized avatar

Rainer Stropek rstropek

View GitHub Profile
@rstropek
rstropek / 010-MainWindow.xaml
Last active April 14, 2021 13:36
XAML Intro
<Window x:Class="HelloXaml.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:HelloXaml"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<!--
+=== Will become an object of this class
@rstropek
rstropek / MainWindow.xaml
Last active May 20, 2021 08:22
Machine Monitor Sample (XAML)
<Window x:Class="MachineMonitor.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MachineMonitor"
mc:Ignorable="d"
Title="Machine Monitor" Height="450" Width="800">
<Window.Resources>
<Style TargetType="local:TemperatureChart">
using System;
var p = new Person("Foo", "Bar", 42);
// The following line does not work because records are immutable
// p.LastName = "Baz";
Console.WriteLine(p.FirstName);
var b = new Product("Bike", "Mountainbike", 499m);
Console.WriteLine(b.Name);
using System;
var v1 = new Vector2d(1d, 2d);
v1.X = 3d; // This works because get and set are generated by default
Console.WriteLine(v1.X);
Console.WriteLine(v1); // record structs implement ToString
var v2 = v1 with { X = 4d }; // We can use the with keyword
Console.WriteLine(v2.X);
using System;
using System.Text;
var p1 = new Point(1d, 2d);
// p1.X = 3d; // This does not work because readonly records are immutable
Console.WriteLine(p1);
// Note: At the time of writing, deconstruct can be compiled on sharplab.io, but cannot be executed.
// If you want to run this code on sharplab.io, comment the following statement.
var (x, y) = p1; // Deconstruction works similar to record classes.
using System;
ReadOnlySpan<Vector2d> vectors = stackalloc Vector2d[] { new(1d, 1d), new(2d, 2d), };
Console.WriteLine(AddAll(vectors));
static T AddAll<T>(ReadOnlySpan<T> addables) where T: IAddable<T>
{
var result = T.Zero;
foreach (var a in addables) result += a;
return result;
using System;
var app = new EndpointConventionBuilder();
// Traditional way of defining a function with an attribute
[HttpGet("/")] int GetAnswer() => 42;
app.MapAction((Func<int>)GetAnswer);
// Now, we can remove the type cast:
app.MapAction(GetAnswer);
#nullable enable
using System;
using System.Runtime.CompilerServices;
var x = 5;
Verify.Lower(x * 2, Convert.ToInt32(Math.Floor(Math.PI)));
public static class Verify
{
using System;
const string s1 = $"abc";
const string s2 = $"{s1}edf";
Console.WriteLine(s2);
DoSomething_Old(42);
DoSomething_VeryOld(42);
[Obsolete($"Use {nameof(DoSomething_New)} instead")]
using System;
using System.Collections.Generic;
var numbers = new List<int>() { 1, 2, 3, 5, 8 };
// List pattern
if (numbers is { 1, 2, 3, 5, 8 })
{
Console.WriteLine("Fibonacci");
}