C#中的C++联合
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/126781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ union in C#
提问by Viktor Elofsson
I'm translating a library written in C++ to C#, and the keyword 'union' exists once. In a struct.
我正在将用 C++ 编写的库翻译成 C#,并且关键字“union”存在一次。在一个结构中。
What's the correct way of translating it into C#? And what does it do? It looks something like this;
将它翻译成 C# 的正确方法是什么?它有什么作用?它看起来像这样;
struct Foo {
float bar;
union {
int killroy;
float fubar;
} as;
}
采纳答案by Armin Ronacher
You can use explicit field layouts for that:
您可以为此使用显式字段布局:
[StructLayout(LayoutKind.Explicit)]
public struct SampleUnion
{
[FieldOffset(0)] public float bar;
[FieldOffset(4)] public int killroy;
[FieldOffset(4)] public float fubar;
}
Untested. The idea is that two variables have the same position in your struct. You can of course only use one of them.
未经测试。这个想法是两个变量在您的结构中具有相同的位置。您当然只能使用其中之一。
More informations about unions in struct tutorial
结构教程中有关联合的更多信息
回答by Nir
In C/C++ union is used to overlay different members in the same memory location, so if you have a union of an int and a float they both use the same 4 bytes of memory to store, obviously writing to one corrupts the other (since int and float have different bit layout).
在 C/C++ 中,联合用于覆盖同一内存位置中的不同成员,所以如果你有一个 int 和一个浮点的联合,它们都使用相同的 4 字节内存来存储,显然写入一个会损坏另一个(因为int 和 float 具有不同的位布局)。
In .Net Microsoft went with the safer choice and didn't include this feature.
在 .Net 中,Microsoft 选择了更安全的选择,并且没有包含此功能。
EDIT: except for interop
编辑:互操作除外
回答by ckramer
Personally, I would ignore the UNION all together and implement Killroy and Fubar as separate fields
就个人而言,我会一起忽略 UNION 并将 Killroy 和 Fubar 实现为单独的字段
public struct Foo
{
float bar;
int Kilroy;
float Fubar;
}
Using a UNION saves 32 bits of memory allocated by the int....not going to make or break an app these days.
使用 UNION 可以节省由 int 分配的 32 位内存......这些天不会成就或破坏应用程序。
回答by Steve Fallows
You can't really decide how to deal with this without knowing something about how it is used. If it is merely being used to save space, then you can ignore it and just use a struct.
如果不了解它的使用方式,您就无法真正决定如何处理它。如果它只是用来节省空间,那么你可以忽略它,只使用一个结构体。
However that is not usually why unions are used. There two common reasons to use them. One is to provide 2 or more ways to access the same data. For instance, a union of an int and an array of 4 bytes is one (of many) ways to separate out the bytes of a 32 bit integer.
然而,这通常不是使用联合的原因。使用它们有两个常见的原因。一种是提供两种或多种方式来访问相同的数据。例如,一个 int 和一个 4 字节数组的联合是一种(多种)分离 32 位整数字节的方法。
The other is when the data in the struct came from an external source such as a network data packet. Usually one element of the struct enclosing the union is an ID that tells you which flavor of the union is in effect.
另一种是结构体中的数据来自网络数据包等外部来源。通常,包含联合的结构体的一个元素是一个 ID,它告诉您联合的哪种风格有效。
In neither of these cases can you blindly ignore the union and convert it to a struct where the two (or more) fields do not coincide.
在这两种情况下,您都不能盲目地忽略联合并将其转换为两个(或更多)字段不重合的结构。
回答by Steve Lillis
If you're using the union
to map the bytes of one of the types to the other then in C# you can use BitConverter
instead.
如果您使用 将union
一种类型的字节映射到另一种类型,则可以在 C# 中使用BitConverter
。
float fubar = 125f;
int killroy = BitConverter.ToInt32(BitConverter.GetBytes(fubar), 0);
or;
或者;
int killroy = 125;
float fubar = BitConverter.ToSingle(BitConverter.GetBytes(killroy), 0);
回答by Yan Chen
public class Foo
{
public float bar;
public int killroy;
public float fubar
{
get{ return (float)killroy;}
set{ killroy = (int)value;}
}
}