コガネブログ

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

【C#】関数の戻り値で ValueTuple を使用する

概要 private (int id, string name) GetCharacter() { return ( 25, "ピカチュウ" ); } 関数の戻り値で ValueTuple を使用することで var character = GetCharacter(); Console.WriteLine( character.id + ", " + character.name ); var ( id, name ) = Get…

【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>…

【Unity】LINQ の結果を匿名型ではなく ValueTuple で受け取る

概要 using System.Linq; using UnityEngine; public class Example : MonoBehaviour { private static string[] m_list = { "フシギダネ", "フシギソウ", "フシギバナ", }; private void Update() { // 匿名型 var r1 = m_list.Select( c => new { name = c…

【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#】Dictionary のキーに複数の値を設定する方法

構造体を使用する方法 using System; using System.Collections.Generic; public static class Program { // 構造体でキー用のデータを定義 public struct KeyData { public int m_series; public int m_number; public KeyData( int series, int number ) {…