The best way to implement a structure like a Union

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

I would like to have a structure similar to C++ Union. What would be the
best way to do it in C#?

Thanks
EitanB
 
For example:
C++:
union Foo
{
int i;
bool b;
};
C#:
using System.Runtime.InteropServices;
//C++ TO C# CONVERTER TODO TASK: Unions are not supported in C#, but the
following union can be simulated with the StructLayout and FieldOffset
attributes.
//ORIGINAL LINE: union Foo
[StructLayout(LayoutKind.Explicit)]
public struct Foo
{
[FieldOffset(0)]
public int i;
[FieldOffset(0)]
public bool b;
}
--
David Anton
http://www.tangiblesoftwaresolutions.com
Convert between VB, C#, and C++
Instant C#
Instant VB
Instant C++
C++ to C# Converter
C++ to VB Converter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top