By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,670 Members | 1,431 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,670 IT Pros & Developers. It's quick & easy.

union in c#

P: n/a
Ali
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
Nov 15 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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" <mo*********@yahoo.com> wrote in message
news:6D**********************************@microsof t.com...
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

Nov 15 '05 #2

P: n/a
Hi,
With the only one exception: You cannot have fields of a refence type in the
union

--
B\rgds
100

"Aravind C" <ar***********@nospam.hotmail.com> wrote in message
news:uT**************@TK2MSFTNGP12.phx.gbl...
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" <mo*********@yahoo.com> wrote in message
news:6D**********************************@microsof t.com...
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


Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.