loway1994 2016-10-10 03:20 采纳率: 100%
浏览 1696
已采纳

c++中的联合体在c#中怎样去实现?

比如有这样的c++代码:
struct A
{
...
...
union
{
...
...
}
}
用c#怎样实现

  • 写回答

1条回答 默认 最新

  • threenewbee 2016-10-10 03:22
    关注
     C#没有联合体,不过可以用属性实现相似的功能
    C++的union
    union MyUnion
    {
    int x;
    struct
    {
    short hi;
    short low;
    } value;
    }
    用C#可以写
    struct MyUnion
    {
    public int x;
    public short hi { get { return x / 65536; } set { x = value * 65536 + x % 65536; } }
    public short low { get { return x % 65536 } set { x = x - x % 65536 + value; } }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?