征服Unity3dunity3D技术分享csharp

Unity中有关C#的几个要点

2018-02-26  本文已影响144人  Nick_Can

大致总结下C#中几个常见知识点的个人理解,以对基础已经有所了解为前提。

ref/out

void RefOutTest()
{
    int a;
    // refTest(ref a);//error: a需要先初始化
    a = 1;
    refTest(ref a);

    outTest(out a);

    Debug.Log("out - " + a);// out - 2
}

void RefTest(ref int a)
{
}

void OutTest(out int a)
{
    a = 2;
}

string

string str1 = "abc";
string str2 = "abc";
Debug.Log(object.ReferenceEquals(str1, str2)); //true
Debug.Log(object.ReferenceEquals(str1, "abc")); //true!!

装拆箱

迭代器

void Start()
{
    var enumerator = EnumerableTest().GetEnumerator();

    Debug.LogError(enumerator.Current);
    Debug.LogError(enumerator.MoveNext());
    Debug.LogError(enumerator.Current);

    //两个输出结果一致
    StartCoroutine(EnumerableTest().GetEnumerator());
    StartCoroutine(EnumeratorTest());
}

IEnumerable EnumerableTest()
{
    Debug.Log(1);
    yield return new WaitForSeconds(.5f);
    Debug.Log(2);
    yield return new WaitForSeconds(.5f);
    Debug.Log(3);
}

IEnumerator EnumeratorTest()
{
    var enumerator = EnumerableTest().GetEnumerator();
    while(enumerator.MoveNext())
    {
        yield return enumerator.Current;
    }
}

GC(Garbage Collector 垃圾收集器)

委托和事件

闭包

容器(数据结构)

Array 数组

ArrayList 数组

List<T> 数组

LinkedList<T> 链表

Queue<T> 队列

Stack<T> 栈

HashTable 散列表

Dictionary<K,T> 字典

使用

上一篇下一篇

猜你喜欢

热点阅读