ソースコード 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…
参考サイト様
ソースコード 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 ) < …
概要 using System; using System.Threading; using System.Threading.Tasks; using UnityEngine; public sealed class Example : MonoBehaviour { private readonly CancellationTokenSource m_cancellationTokenSource = new(); private async void Start(…
概要 using System.Linq; using UnityEngine; public class Example : MonoBehaviour { private void Awake() { var list1 = new[] { 1, 2, 3 }; var list2 = new[] { 1, 1, 1 }; Debug.Log( list1.Distinct().Count() == 1 ); // False Debug.Log( list2.Di…
概要 /// <summary> /// 継承しているすべての基底クラスと実装しているすべてのインターフェイスの Type を返します /// </summary> public static IEnumerable<Type> GetParentTypes( this Type self ) { if ( self == null ) yield break; foreach ( var x in self.GetInterfaces()</type>…
概要 /// <summary> /// 指定された基底クラスを継承しているもしくはインターフェイスを実装している場合 true を返します /// </summary> public static bool IsInherits( this Type self, Type baseOrInterfaceType ) { if ( self == null ) return false; if ( baseOrInterf…
ソースコード using System.Reflection; using System.Runtime.CompilerServices; namespace Kogane { public static class MethodInfoExtensionMethods { public static bool IsExtensionMethod( this MethodInfo self ) { return self.IsDefined( typeof( …
ソースコード public static class TypeExtensionMethods { public static bool IsNullable( this Type self ) { return Nullable.GetUnderlyingType( self ) != null; } }
拡張メソッド 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…
概要 using System.Threading; using UnityEngine; public class Example : MonoBehaviour { private void Awake() { Debug.Log( CancellationToken.None == default ); // True } }
ソースコード using System; using System.Collections.Generic; namespace Kogane { // ReSharper disable once InconsistentNaming public static class IEnumerableExtensionMethods { public static bool TryElementAt<TSource> ( this IEnumerable<TSource> source, int i</tsource></tsource>…
ソースコード using System; namespace Kogane { public static class TypeUtils { public static string GetCSharpTypeKeyword( Type type ) { return GetCSharpTypeKeyword( type.Name ); } // https://stackoverflow.com/questions/56352299/gettype-retu…
ソースコード using System; using System.Linq; namespace Kogane { public static class TypeUtils { // https://stackoverflow.com/questions/1533115/get-generictype-name-in-good-format-using-reflection-on-c-sharp public static string GetPrettyN…
ソースコード namespace Kogane { public static class CharExtensionMethods { public static bool IsAlphanumeric( this char self ) { return char.IsLetterOrDigit( self ); } } } 使用例 Debug.Log( '0'.IsAlphanumeric() ); // True Debug.Log( 'A'.Is…
ソースコード namespace Kogane { public static class StringExtensionMethods { public static bool IsAlphanumeric( this string self ) { for ( var i = 0; i < self.Length; i++ ) { var x = self[ i ]; if ( !char.IsLetterOrDigit( x ) ) { return fa…
ソースコード 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>…
概要 var isAlphanumeric = char.IsLetterOrDigit( 'A' ); // True char.IsLetterOrDigit を使うと判定できる 0 ~ 9、A ~ Z、a ~ z は True になる 記号や半角スペースは False になる 使用例 // 0 ~ 9、A ~ Z、a ~ z は True になる var isAlphanumeric = "…
ソースコード 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…
概要 private static string ToLiteralString( object value ) { if ( value == null ) return "null"; if ( value is bool boolValue ) return boolValue ? "true" : "false"; if ( value is float floatValue ) return $"{floatValue}f"; if ( value is lo…
概要 C# 9.0 までは引数なしコンストラクタを定義できないため 構造体のフィールドに初期値を設定できない 代替案としてフィールドを nullable にして プロパティの getter でフィールドが null なら初期値を設定する方法がある using UnityEngine; public s…
概要 // すべて True Debug.Log( typeof( bool ).IsValueType ); Debug.Log( typeof( byte ).IsValueType ); Debug.Log( typeof( sbyte ).IsValueType ); Debug.Log( typeof( char ).IsValueType ); Debug.Log( typeof( decimal ).IsValueType ); Debug.Log(…
概要 var array1 = new int[ 0 ]; var array2 = new string[ 0 ]; var array3 = new Vector3[ 0 ]; Debug.Log( array1.GetType().IsArray ); Debug.Log( array2.GetType().IsArray ); Debug.Log( array3.GetType().IsArray ); 参考サイト様
概要 var dictionary1 = new Dictionary<int, string>(); var dictionary2 = new Dictionary<int, Vector3>(); Debug.Log( typeof( Dictionary<,> ).IsAssignableFrom( dictionary1.GetType().GetGenericTypeDefinition() ) ); Debug.Log( typeof( Dictionary<,> ).IsAssignableFrom( di</int,></int,>…
概要 参考サイト様
概要 var text = Regex.Replace( text, "<.*?>", string.Empty );
概要 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,>…
概要 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>…
概要 using System.Text.Json; using UnityEngine; public enum CharacterType { NONE, PLAYER, ENEMY, } public class Example : MonoBehaviour { private void Start() { var data = new { characterType = CharacterType.PLAYER }; Debug.Log( JsonSerial…
概要 System.HashCode を使用すると GetHashCode を簡単に実装できる using System; using UnityEngine; public sealed class Character { public int Id { get; } public string Name { get; } public Character ( int id, string name ) { Id = id; Name =…