Skip to content

Instantly share code, notes, and snippets.

View hsytkm's full-sized avatar
💪
Our ultimate goal is to make a living doing development, right?

thinva hsytkm

💪
Our ultimate goal is to make a living doing development, right?
View GitHub Profile
@hsytkm
hsytkm / Id.cs
Created December 2, 2020 10:49
Manage multiple id.
// 複数のIDを管理するテク
// 【C# 9.0】 Source Generator回 - ufcpp https://youtu.be/QXT-TSvIbbY?t=5816
var person0 = new Person(0);
var person1 = new Person(1);
var shop0 = new Shop(0);
System.Console.WriteLine("Check Id0 : " + (person0.Id == person1.Id));
// 以下だと型が違うので比較できない
@hsytkm
hsytkm / IsDebugBuild.cs
Last active January 30, 2021 14:18
How to write "#if DEBUG"
// これでも問題ないけど、インテリセンスが効かなくてダサい
#if DEBUG
System.Console.WriteLine("Debug build dane.");
#else
System.Console.WriteLine("Release build dane.");
#endif
// こちらだとインテリセンスが効く
// しかも const 定義の if 文は IL で消してくれる (ILSpyで確認済み)
if (AssemblyState.IsDebugBuild)
@hsytkm
hsytkm / CheckStringLength.cs
Last active December 15, 2020 14:02
Check string.Length @ C#9.0
/* https://twitter.com/_midoliy_/status/1336099984962330624
*
* 以下の書き方ができるらしい。
*
* C#9.0 の記事を色々見てたけど初めて見た。
* F# っぽい構文らしいけど、C# 側には説明ないのかな?
*
* nullチェックも行われるので、覚えていると良さげ。(SharpLabで確認)
*
* ~~~追記ここから~~~
@hsytkm
hsytkm / PooledArray.cs
Last active December 21, 2020 10:07
Access array items via ref
/* - https://github.com/hsytkm/CSharpStandardSamples/blob/master/CSharpStandardSamples.Core/Spans/PooledArray.cs
*
* - Pooling large arrays with ArrayPool https://adamsitnik.com/Array-Pool/
* that it has a default max array length, equal to 2^20 (1024*1024 = 1 048 576).
*
* - LitJWTに見るモダンなC#のbyte[]とSpan操作法 http://neue.cc/2019/05/27_578.html
* ようするに、今どきnew byte[]なんてしたら殺されるぞ!
*
* - https://gist.github.com/ufcpp/b0853cff5823d49306ba693aaa5c39fb
*/
@hsytkm
hsytkm / Deconstruction.cs
Created December 27, 2020 09:30
Deconstruction Tuple and AnonymousClass
#nullable enable
using System;
// tuple
var t = (x: 1, y: 2);
if (t is (x: var tx1, y: var ty1))
{
Console.WriteLine($"Tuple Deconstruct : x + y = {tx1 + ty1}");
@hsytkm
hsytkm / StringIntern.cs
Created December 28, 2020 10:31
C# String.Intern()
// String.InternによるUnityでの省メモリ化ハック http://engineering.grani.jp/entry/2017/10/12/220409
#nullable enable
using System;
using System.Text;
unsafe
{
/* 1. 文字列リテラルはインターンプールに登録される。
@hsytkm
hsytkm / EnumWhenAll.cs
Created January 4, 2021 03:59
Linq in the asynchronous era
// 非同期時代のLINQ http://neue.cc/2013/12/04_435.html
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
static string GetName(int id) => $"Hoge {id}";
@hsytkm
hsytkm / CallStaticCtor.cs
Created January 17, 2021 07:27
Call static ctor directly.
using System;
using System.Runtime.CompilerServices;
// static ctor を直接呼び出す(Class って名前だけど struct もOK)
RuntimeHelpers.RunClassConstructor(typeof(MyData).TypeHandle);
Console.WriteLine("end");
struct MyData
{
@hsytkm
hsytkm / AppHasModelInstance.cs
Created March 28, 2021 09:42
App has model instance.
// AppクラスにModelインスタンスを持たせる
// https://github.com/runceel/Livet-samples/blob/master/CommunicationBetweenViewModels/App.xaml.cs
public partial class App : Application
{
public static new App Current => (App)Application.Current;
internal MyModel MyModel { get; } = new();
}
@hsytkm
hsytkm / RxFirstAsyncVsTake1.cs
Created May 1, 2021 07:51
[Rx]FirstAsync() vs FirstOrDefaultAsync() vs Take(1)
using System;
using System.Reactive;
using System.Reactive.Linq;
using System.Reactive.Subjects;
Console.WriteLine("データ0個で完了");
var subject0 = new Subject<Unit>();
subject0.FirstAsync().Subscribe(new Dump<Unit>("Empty.FirstAsync"));
subject0.FirstOrDefaultAsync().Subscribe(new Dump<Unit>("Empty.FirstOrDefaultAsync"));
subject0.Take(1).Subscribe(new Dump<Unit>("Empty.Take"));