コガネブログ

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

【C#】値をスワップする関数を自作する

ソースコード

public static class GenericUtils
{
    public static void Swap<T>( ref T a, ref T b )
    {
        var tmp = a;
        a = b;
        b = tmp;
    }
}

使用例

using UnityEngine;

public class Example : MonoBehaviour
{
    private void Awake()
    {
        int a = 1;
        int b = 2;

        GenericUtils.Swap( ref a, ref b );

        Debug.Log( a );
        Debug.Log( b );
    }
}