C언어의 union
1
2
3
4
5
union MyUnion
{
int intValue; // 4바이트
short shortValue; // 2바이트
}
- 위처럼 C언어에는 서로 다른 타입의 변수가 동일 메모리를 사용하도록 하는
union(공용체)이 존재한다.
C#에서의 union
C#에는union키워드가 존재하지 않지만, 편법을 통해union기능을 구현할 수 있다.
1
2
3
4
5
6
7
8
9
10
11
12
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Explicit)]
class UnionExample
{
[System.Runtime.InteropServices.FieldOffset(0)]
public int intValue;
[System.Runtime.InteropServices.FieldOffset(0)]
public short shortValue1;
[System.Runtime.InteropServices.FieldOffset(2)]
public short shortValue2;
}
위의 클래스에서는 0 ~ 2바이트 부분을 intValue의 4바이트 중 앞의 2바이트와 shortValue1이 공유하며
2 ~ 4바이트 부분을 intValue의 4바이트 중 뒤의 2바이트와 shortValue2가 공유하게 된다.
이렇게 원하는 변수가 특정 메모리 영역을 바이트 단위로 구분하여 사용하도록 할 수 있다.
참고 : 객체의 크기 참조
-
StructLayout애트리뷰트로LayoutKind를Explicit또는Sequential로 지정한 경우에만Marshal을 이용해 크기를 참조할 수 있다. -
필드에 스트링, 배열, 컬렉션 등의 객체가 존재하는 경우에는 불가능하다.
1
2
3
4
UnionExample a = new UnionExample();
// 객체의 크기 참조
int size = System.Runtime.InteropServices.Marshal.SizeOf(a);