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
<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 |
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
<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"> |
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; | |
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); |
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; | |
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); |
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.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. |
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; | |
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; |
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; | |
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); |
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
#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 | |
{ |
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; | |
const string s1 = $"abc"; | |
const string s2 = $"{s1}edf"; | |
Console.WriteLine(s2); | |
DoSomething_Old(42); | |
DoSomething_VeryOld(42); | |
[Obsolete($"Use {nameof(DoSomething_New)} instead")] |
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.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"); | |
} |