c# GC 新典型

public class testGC : MonoBehaviour
{
    class XDict<K, V>
    {
        public void TryGetValue(K key, V value)
        {
            if(key == null) //产生 GC-ALLOC,因为实参为int,而 null是引用类型,发生了装箱操作
            {
            }
        }
    }

    XDict<int, string> mDict = new XDict<int, string>();

    void Start()
    {
    }

    void Update()
    {
        var key = 10;
        if (key != null) //不会产生GC-ALLOC
        {
            var value = 20;
        }

        mDict.TryGetValue(10, "hello");
    }
}

 

posted @ 2019-02-18 12:20  时空观察者9号  阅读(200)  评论(0编辑  收藏  举报