Unity中對C#泛型的使用

比較簡單就不囉嗦了,直接上段小代碼,清晰明瞭!


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class T_test : MonoBehaviour {

    private void Start()
    {
        Test<int>();
        Debug.Log("-------------");
        Test<string>();
    }

    private void Test<T>()
    {
        if (Types.Equals(typeof(T),typeof(int)))
        {
            Debug.Log("調用的類型是  int");
        }
        else if (Types.Equals(typeof(T), typeof(string)))
        {
            Debug.Log("調用的類型是  string");
        }
    }
}

運行結果:






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章