コガネブログ

平日更新を目標に Unity や C#、Visual Studio、ReSharper などのゲーム開発アレコレを書いていきます

【C#】最初に見つかった文字列だけ置換する string.Replace

ソースコード public static string ReplaceFirst ( this string self, string oldValue, string newValue ) { var startIndex = self.IndexOf( oldValue ); if ( startIndex == -1 ) return self; return self .Remove( startIndex, oldValue.Length ) .Ins…

【C#】float の値が 0 かどうかを確認する拡張メソッド

ソースコード using System; public static class FloatExtensions { public static bool IsZero( this float self ) { return self.IsZero( float.Epsilon ); } public static bool IsZero( this float self, float epsilon ) { return Math.Abs( self ) < …

【C#】NPOI でどんなセルの値も文字列で取得できる拡張メソッド

拡張メソッド using NPOI.SS.UserModel; namespace Kogane { public static class ICellExtensionMethods { public static string GetValue( this ICell self ) { if ( self == null ) return string.Empty; return self.CellType switch { CellType.Numeric…

【Unity】Java の Stream API の Peek のような機能を LINQ で使えるようにする拡張メソッド

ソースコード using System; using System.Collections.Generic; using JetBrains.Annotations; namespace Kogane { public static class IEnumerablePeekExtensionMethods { public static IEnumerable<T> Peek<T> ( [NotNull] this IEnumerable<T> source, [NotNull</t></t></t>…

【Unity】LINQ で抽出した要素をログ出力して確認できるようにする拡張メソッド

ソースコード using System; using System.Collections.Generic; using JetBrains.Annotations; using UnityEngine; namespace Kogane { public static class IEnumerableLogExtensionMethods { public delegate void LogDelegate( object item ); public st…

【C#】2 つの配列の要素をインデックス同士で紐付けて Tuple にまとめる拡張メソッド

概要 using System.Collections.Generic; namespace Kogane { public static class SequenceJoinTupleExtensionMethods { public static (T1, T2)[] JoinTuple<T1, T2>( this T1[] self, T2[] other ) { var count = self.Length; var result = new (T1, T2)[ count </t1,>…

【C#】条件を満たす場合にのみ Prepend する拡張メソッド

概要 using System.Collections.Generic; using System.Linq; namespace Kogane { public static class EnumerableExtensionMethods { public static IEnumerable<T> PrependIf<T> ( this IEnumerable<T> self, bool conditional, T element ) { return !conditional </t></t></t>…

【C#】シーケンスの要素が重複している場合 true を返す拡張メソッド

ソースコード using System; using System.Collections.Generic; using System.Linq; public static class IEnumerableExt { public static bool HasDuplication<TKey, TSource> ( this IEnumerable<TSource> self, Func<TSource, TKey> keySelector ) { return self .GroupBy( keySelector ) .Any( </tsource,></tsource></tkey,>…

【C#】数値の桁数を取得する拡張メソッド

ソースコード using System; public static class IntExt { public static int GetDigits( this int num ) { return num == 0 ? 1 : ( int ) Math.Log10( num ) + 1; } } 使用例 using System; public static class Program { private static void Main() { …

【C#】シーケンスから重複している要素を取得する拡張メソッド

ソースコード using System; using System.Collections.Generic; using System.Linq; public static class IEnumerableExt { public static IEnumerable<TKey> GetDuplication<TKey, TSource> ( this IEnumerable<TSource> self, Func<TSource, TKey> keySelector ) { return self .GroupBy( keySelector )</tsource,></tsource></tkey,></tkey>…

【C#】ValueTuple を使用して二重ループを一重ループで記述できるようにする拡張メソッド

ソースコード using System.Collections.Generic; using System.Linq; public static class IEnumerableExt { public static IEnumerable<(T item1, T item2)> Combine<T> ( this IEnumerable<T> first, IEnumerable<T> second ) { return first.SelectMany( i1 => se</t></t></t>…

【C#】配列やリストから重複組合せを取得できる拡張メソッド

はじめに 上記サイト様が、リストから組み合わせを取得できる関数を公開されております こちらを少し修正して、配列やリストから重複組合せを取得できる拡張メソッドを作りました ソースコード using System.Collections.Generic; using System.Linq; public…

【C#】配列やリストから組合せを取得できる拡張メソッド

はじめに 上記サイト様が、リストから組み合わせを取得できる関数を公開されております こちらを少し修正して、配列やリストから組合せを取得できる拡張メソッドを作りました ソースコード using System.Collections.Generic; using System.Linq; public sta…

【C#】リフレクションで引数に params が付いているかどうか確認できる拡張メソッド

ソースコード using System; using System.Reflection; public static class ParameterInfoExt { public static bool HasParams( this ParameterInfo param ) { var type = typeof( ParamArrayAttribute ); return param.GetCustomAttributes( type, false )…

【C#】Type 型で組み込み型のエイリアス名を取得できる拡張メソッド

ソースコード using System; public static class TypeExt { public static string GetAliasName( this Type self ) { switch ( self.FullName ) { case "System.Boolean": return "bool"; case "System.Byte": return "byte"; case "System.SByte": return …

【Unity】List.Find の null チェックを少しだけ簡潔に記述できる拡張メソッド

ソースコード using System; using System.Collections.Generic; public static class ListExt { public static bool TryFind<T>( this List<T> self, Predicate<T> match, out T result ) where T : class { result = self.Find( match ); return result != null; } </t></t></t>…

【C#】二次元配列を回転させる拡張メソッド

はじめに 上記サイト様が公開されているプログラムを参考に 二次元配列を回転させる拡張メソッドを作成しました ソースコード public static class ArrayExt { // 時計回りに 90 度回転 public static T[,] RotateClockwise<T>( this T[,] self ) { int rows = </t>…

【C#】文字列から数値を抽出する拡張メソッド

ソースコード using System.Text.RegularExpressions; public static class StringExt { public static int ExtractInteger( this string self ) { return int.Parse( Regex.Replace( self, @"[^0-9]", string.Empty ) ); } } 使い方 var str1 = "s012"; Con…

【C#】高速な列挙型の API を使用できる「FastEnum」紹介

はじめに 「FastEnum」をプロジェクトに追加することで 高速な列挙型の API を使用できるようになります インストール Visual Studio メニューの「ツール>NuGet パッケージ マネージャー> ソリューションの NuGet パッケージの管理」を選択します 「参照」タ…

【C#】高速な LINQ を使用できる「FastLinq」紹介

はじめに 「FastLinq」をプロジェクトに追加することで 高速な LINQ を使用できるようになります インストール Visual Studio メニューの「ツール>NuGet パッケージ マネージャー> ソリューションの NuGet パッケージの管理」を選択します 「参照」タブを選…

【C#】配列やリストの分解代入を可能にする Deconstruct

ソースコード using System.Collections.Generic; using System.Linq; public static class IListExt { public static void Deconstruct<T> ( this IList<T> self, out T first, out IList<T> rest ) { first = self.Count > 0 ? self[ 0 ] : default; rest = self.Sk</t></t></t>…

【C#】ValueTuple を使用して配列やリストの foreach で簡単にインデックスを取得できる拡張メソッド

ソースコード using System; using System.Collections.Generic; public static class IEnumerableExt { public static IEnumerable<(int index, T value)> WithIndex<T> ( this IEnumerable<T> source ) { if ( source == null ) { throw new ArgumentNullExcepti</t></t>…

【C#】可変長引数を使用できる Enumerable.Concat

ソースコード using System.Collections.Generic; using System.Linq; public static class IEnumerableExt { public static IEnumerable<T> Concat<T> ( this IEnumerable<T> first, params T[] second ) { return Enumerable.Concat( first, second ); } } 使用例 </t></t></t>…

【C#】シーケンスの要素を条件を満たすものと満たさないものに分ける拡張メソッド

ソースコード using System; using System.Collections.Generic; public static class IEnumerableExt { public static Tuple<IEnumerable<T>, IEnumerable<T>> Partition<T> ( this IEnumerable<T> self, Func<T, bool> predicate ) { var ok = new List<T>(); var ng = new List<T>(); foreach ( v</t></t></t,></t></t></t></ienumerable<t>…

【C#】SelectMany( c => c ) の記述を簡略化する拡張メソッド

ソースコード using System.Collections.Generic; using System.Linq; public static class IEnumerableExt { public static IEnumerable<T> Flatten<T> ( this IEnumerable<IEnumerable<T>> self ) { return self.SelectMany( c => c ); } } 使用例 通常 foreach ( var n in list</ienumerable<t></t></t>…

【C#】Dictionary から指定したキーの要素を取得してから削除する拡張メソッド

ソースコード using System.Collections.Generic; public static class DictionaryExt { public static bool Remove<TKey, TValue> ( this Dictionary<TKey, TValue> self, TKey key, out TValue value ) { self.TryGetValue( key, out value ); return self.Remove( key ); } } 使用方法</tkey,></tkey,>…

【C#】IEnumerable からランダムに値を取得する拡張メソッド

ソースコード System.Random 版 using System; using System.Collections.Generic; using System.Linq; public static partial class IEnumerableExt { private static readonly Random m_random = new Random(); public static T RandomAt<T>( this IEnumerable<T></t></t>…

【C#】高速な LINQ を使用できる「LinqFaster」紹介

はじめに 「LinqFaster」をプロジェクトに追加することで 高速な LINQ を使用できるようになります インストール Visual Studio メニューの「ツール>NuGet パッケージ マネージャー> ソリューションの NuGet パッケージの管理」を選択します 「参照」タブを…

【C#】文字列がすべて小文字かどうかを判定する拡張メソッド

ソースコード public static class StringExt { public static bool IsLower( this string self ) { for ( int i = 0; i < self.Length; i++ ) { if ( char.IsUpper( self[ i ] ) ) { return false; } } return true; } } 使い方 var str = "abcde"; var isL…

【C#】文字列がすべて大文字かどうかを判定する拡張メソッド

ソースコード public static class StringExt { public static bool IsUpper( this string self ) { for ( int i = 0; i < self.Length; i++ ) { if ( char.IsLower( self[ i ] ) ) { return false; } } return true; } } 使い方 var str = "ABCDE"; var isU…