Skip to content

Instantly share code, notes, and snippets.

View ufcpp's full-sized avatar

Nobuyuki Iwanaga ufcpp

View GitHub Profile
@ufcpp
ufcpp / PCParamegers.cs
Created September 15, 2023 08:22
プライマリコンストラクター引数のキャプチャ
class A(int x)
{
int _x = x; // これはキャプチャしない。
}
class B(int x)
{
public int X { get; } = x; // これもキャプチャしない。
}
@ufcpp
ufcpp / Program.cs
Created July 26, 2023 02:07
default リテラルのオーバーロード解決?
X.M(0); // 整数リテラルの自然な型は int
X.M(default); // でも、 default は「サイズが小さい型優先」っぽい
// そういう仕様?意図的?
class X
{
public static void M(int i32) { }
public static void M(short i16) { }
}
@ufcpp
ufcpp / StringComparison.cs
Created June 14, 2023 10:51
ひどい。
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 になる効果もあり。)
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)
using System.Buffers.Binary;
var x = 0x12345678;
Console.WriteLine($"{x:X}");
Console.WriteLine($"{x.Little()}");
static class LittleEndianExtensions
{
public static LittleEndianInt32 Little(this int x) => new(x);
@ufcpp
ufcpp / Comparison.cs
Created March 11, 2023 14:59
A<B, C>(D)
class Program
{
static void X(bool x) { }
static void X(bool x, bool y) { }
static bool A<T1, T2>(int x) => true;
class B { }
class C { }
static void Main()
{
@ufcpp
ufcpp / ConditionalNestedInterpolation.cs
Created January 14, 2023 01:45
IfTrue/IfFalse interpolated string handler
using System.Globalization;
using System.Runtime.CompilerServices;
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
static string m(bool condition) => string.Create(
CultureInfo.GetCultureInfo("fr-fr"),
$"direct: {1.5} or {1:c}, nested: {If(condition, $"{1.5}", $"{1:C}")}");
Console.WriteLine(m(true));
@ufcpp
ufcpp / IDefaultSpanFormattable.cs
Created January 7, 2023 14:56
ISpanFormattable default implementation of IFormattable.ToString
using System.Buffers;
public interface IDefaultSpanFormattable : ISpanFormattable
{
string IFormattable.ToString(string? format, IFormatProvider? formatProvider)
{
var buffer = (stackalloc char[512]);
if (TryFormat(buffer, out var charsWritten, format, formatProvider))
{
return new string(buffer[..charsWritten]);
@ufcpp
ufcpp / RefLinkedList.cs
Created December 12, 2022 15:32
ref linked list
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
var nodes = new Node<string>[]
{
new("a0", 2),
new("a1", 5),
new("a2", 1),
new("a3", -2),
new("a4", 3),
@ufcpp
ufcpp / tt2rawstring.cs
Last active December 7, 2022 13:28
Convert T4 to StringBuilder.Append($$""" """);
using System.Text.RegularExpressions;
using System.Windows;
class Program
{
[STAThread]
static void Main()
{
var x = Clipboard.GetText();