Why does this nullable int property generate an invalid IL code exception?

时间:2016-04-07 10:35:46

标签: c# unity3d properties nullable

When I attempt to run the following simplified script:

using UnityEngine;
using System.Collections;
public class TestScript : MonoBehaviour
{
    int? _value;
    public int? PostIncrementProperty { get { return _value--; } }
    public int? PreIncrementProperty { get { return --_value; } }

    void Start () 
    {
        _value = 0;
        int? v1 = PostIncrementProperty;
        int? v2 = PreIncrementProperty;
    }
}

Unity displays the following exception:

InvalidProgramException: Invalid IL code in TestScript:get_PostIncrementProperty (): IL_0023: stfld 0x0400002d

Whereas the following test program in VS2012 compiles and functions correctly:

public class Program
{
    int? _value;
    public int? PostIncrementProperty { get { return _value--; } }
    public int? PreIncrementProperty { get { return --_value; } }

    public static void Main(string[] args)
    {
        Program p = new Program();
        p._value = 0;
        int? v1 = p.PostIncrementProperty; // v1 = 0
        int? v2 = p.PreIncrementProperty; // v2 = -2
    }
}

I'm fairly new to Unity (5.0.1f) and Monodevelop (Win7) - is there a simple mistake or misunderstanding I'm making here, or is this a bug?

0 个答案:

没有答案
相关问题