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;
}
}
dXNpbmcgU3lzdGVtOwp1c2luZyBTeXN0ZW0uQ29sbGVjdGlvbnMuR2VuZXJpYzsKdXNpbmcgU3lzdGVtLkxpbnE7CnVzaW5nIFN5c3RlbS5UZXh0OwoKdXNpbmcgU3lzdGVtLlJ1bnRpbWUuSW50ZXJvcFNlcnZpY2VzOwoKbmFtZXNwYWNlIOmFjeWIl+OBruWei+WkieaPm19fCnsKICAgIGNsYXNzIFByb2dyYW0KICAgIHsKICAgICAgICBzdGF0aWMgdm9pZCBNYWluKHN0cmluZ1tdIGFyZ3MpCiAgICAgICAgewogICAgICAgICAgICBVbmlvblJlZmVyZW5jZSBVUiA9IG5ldyBVbmlvblJlZmVyZW5jZSgpOy8v5LiL44Gu5pa544Gr5a6a576p44GC44KK44CCCgogICAgICAgICAgICBVUi5CeXRlQXJyYXkgPSBuZXcgYnl0ZVsxMjhdOwogICAgICAgICAgICBVUi5TaG9ydEFycmF5WzBdID0gNTExOwoKICAgICAgICAgICAgQ29uc29sZS5Xcml0ZUxpbmUoIkJ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