union in c#

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

Guest

Hi,

I'm a new programmer in C# but I know C/C++ programing,

I need somthing like 'union' in C#, what can I do?

thanks Ali
 
Hi Ali,

You can probably try to simulate the equivalent of an union
by setting the [FieldOffset] attribute for all members
in a struct to the 0th position for the offset.

[StructLayout(LayoutKind.Explicit)]
public struct MyUnionWithCategories
{
[FieldOffset(0)] public int Category;
[FieldOffset(0)] public char CategoryRep;
[FieldOffset(0)] public long CategoryIndicator;
}

Regards,
Aravind C
 
Hi,
With the only one exception: You cannot have fields of a refence type in the
union

--
B\rgds
100

Aravind C said:
Hi Ali,

You can probably try to simulate the equivalent of an union
by setting the [FieldOffset] attribute for all members
in a struct to the 0th position for the offset.

[StructLayout(LayoutKind.Explicit)]
public struct MyUnionWithCategories
{
[FieldOffset(0)] public int Category;
[FieldOffset(0)] public char CategoryRep;
[FieldOffset(0)] public long CategoryIndicator;
}

Regards,
Aravind C


Ali said:
Hi,

I'm a new programmer in C# but I know C/C++ programing,

I need somthing like 'union' in C#, what can I do?

thanks Ali
 
Back
Top