This file contains 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
var a = new A(); | |
var b = new B(); | |
var c = new C(); | |
// using 越し、defensive copy されたインスタンスで Dispose 呼ばれるので、元の a, b は書き変わらない。 | |
using (a) using (b) using (c) { } | |
Console.WriteLine(a.X); // false | |
Console.WriteLine(b.X); // false | |
// インターフェイスへのキャストして呼ぶのもダメ。 |
This file contains 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
unsafe | |
{ | |
Circle circle = new() { Radius = 1 }; | |
Rectangle rectangle = new() { Width = 2, Height = 3 }; | |
Rectangle square = new() { Width = 2, Height = 2 }; | |
Console.WriteLine(Algorithm.AreaRatio(0, &circle)); | |
Console.WriteLine(Algorithm.AreaRatio(1, &rectangle)); | |
Console.WriteLine(Algorithm.AreaRatio(1, &square)); |
This file contains 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
for (uint i = 0; i < 12; i++) | |
{ | |
Console.WriteLine(format(i)); | |
} | |
for (uint i = 15; i < 120; i+=7) | |
{ | |
Console.WriteLine(format(i)); | |
} | |
Console.WriteLine(format(1234)); |
This file contains 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.Collections; | |
using System.Runtime.CompilerServices; | |
var d = new Dictionary<int, string> { { 1, "one" }, { 2, "two" } }; | |
ICovariantDictionary<int, string> cd = d.AsCovariant(); | |
// TValue が out なので string → object 代入可能。 | |
ICovariantDictionary<int, object> id = cd; | |
foreach (var (k, v) in id) |
This file contains 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
namespace TimeProviderExtension; | |
public class PausableTimeProvider(TimeProvider innerProvider) : TimeProvider | |
{ | |
public PausableTimeProvider() : this(System) { } | |
private long _pausedTimestamp = innerProvider.GetTimestamp(); | |
private long _offsetTimestamp = 0; | |
private DateTimeOffset _pausedUtcNow = innerProvider.GetUtcNow(); | |
private TimeSpan _offsetUtcNow = default; |
This file contains 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.Runtime.CompilerServices; | |
var p = new Private(); | |
Console.WriteLine(p.Value); // 0 | |
Accessor r = p; // implicit cast で p の参照が r に渡る。 | |
r.Value = 1; | |
Console.WriteLine(p.Value); // 書き変わってる。 | |
// PrivateProxy 触りつつ、 |
This file contains 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
class A(int x) | |
{ | |
int _x = x; // これはキャプチャしない。 | |
} | |
class B(int x) | |
{ | |
public int X { get; } = x; // これもキャプチャしない。 | |
} |
This file contains 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
X.M(0); // 整数リテラルの自然な型は int | |
X.M(default); // でも、 default は「サイズが小さい型優先」っぽい | |
// そういう仕様?意図的? | |
class X | |
{ | |
public static void M(int i32) { } | |
public static void M(short i16) { } | |
} |
This file contains 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.Globalization; | |
using System.Text.RegularExpressions; | |
using static System.Console; | |
var s1 = "a\u0301"; // á = a + ́ | |
var s2 = "\u00e1"; // á | |
// たとえ InvariantCulture でも。 | |
// <InvariantGlobalization>false</InvariantGlobalization> な限り。 | |
// (↑true のときは、StringComparison.Default が Ordinal になる効果もあり。) |
This file contains 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.Buffers.Text; | |
using System.Diagnostics.CodeAnalysis; | |
using System.Runtime.InteropServices; | |
var x = 0x12345678; | |
Console.WriteLine($"{x.Hex()}"); | |
public static class Format | |
{ | |
public static AsciiBuffer8 Hex(this int x) |
NewerOlder