Ejemplo n.º 1
0
    /// <summary>
    /// 测试struct与null的关系
    /// </summary>
    private void TestStructNull()
    {
        //SimpleTestStruct strcutNull = null; // Assets/StructAndClass/StructAndClass.cs(98,33): error CS0037: Cannot convert null to `SimpleTestStruct' because it is a value type

        // SimpleTestStruct simple = new SimpleTestStruct();
        // simple = null; 不能赋空值, 报错同上

        //SimpleTestStruct simple;
        //simple = null; 不能赋空值, 报错同上

        SimpleTestStruct?strcutNull = null;        // V struct 赋空值的正确做法

        Debug.Log(strcutNull.ToString());          // 可以打印,但里面什么也没有
        //if(strcutNull == null)
        //{
        //  可以赋空值,但是不能判空?
        //	Assets / StructAndClass / StructAndClass.cs(99, 12): error CS0037: Cannot convert null to `SimpleTestStruct' because it is a value type
        //}
        strcutNull = new SimpleTestStruct();
        Debug.Log(strcutNull.ToString());         // 默认赋值

        strcutNull = null;
        Debug.Log(strcutNull.ToString());          // 可以再次赋空值

        // TODO 测试strcutNull大小
    }
Ejemplo n.º 2
0
    /// <summary>
    /// 测试struct的构造函数
    /// </summary>
    private void TestStructConstructer()
    {
        TestStruct testConstructor;         // 结构将不会被初始化,但是也不能访问。

        //PrintTestStruct(testConstructor, "no new testConstructor"); // Assets/StructAndClass/StructAndClass.cs(80,19): error CS0165: Use of unassigned local variable `testConstructor'

        testConstructor = new TestStruct();
        //PrintTestStruct(testConstructor, "new testConstructor");
        // 尚未初始化testClass
        // NullReferenceException: Object reference not set to an instance of an object
        // estStruct.ToString()(at Assets / StructAndClass / StructAndClass.cs:193)
        // StructAndClass.PrintTestStruct(TestStruct info, System.String desc)(at Assets / StructAndClass / StructAndClass.cs:136)
        // StructAndClass.TestValueStruct()(at Assets / StructAndClass / StructAndClass.cs:83)
        // StructAndClass.Start()(at Assets / StructAndClass / StructAndClass.cs:15)

        testConstructor = new TestStruct(new TestClass(100));
        PrintTestStruct(testConstructor, "new testConstructor TestClass Param");

        SimpleTestStruct simple = new SimpleTestStruct();

        Debug.Log(simple.ToString());         // 默认赋值
    }