using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; namespace 配列の型変換__ { class Program { static void Main(string[] args) { UnionReference UR = new UnionReference();//下の方に定義あり。 UR.ByteArray = new byte[128]; UR.ShortArray[0] = 511; Console.WriteLine("ByteArray:{0},ShortArray:{1}", UR.ByteArray.Count(), UR.ShortArray.Count()); UR.ByteArray = null; UR.ShortArray = new short[128]; UR.ByteArray[4] =128 ; Console.WriteLine("ByteArray:{0},ShortArray:{1}", UR.ByteArray.Count(), UR.ShortArray.Count()); return; } //static void Main(string[] args) //{ // short[] Ary = new short[] { 1, 2, 3, 4, 5, 6, 7, 8 }; // byte[] Ary2 = Ary;//不可 //} } [System.Runtime.InteropServices.StructLayout(LayoutKind.Explicit)] struct UnionReference { [System.Runtime.InteropServices.FieldOffset(0)] public byte[] ByteArray; [System.Runtime.InteropServices.FieldOffset(0)] public short[] ShortArray; } }