私は通常、C++ コードでこれを行います。
int variable = 10;
int sizeOfVariable = sizeof(variable); //Returns 4 for 32-bit process
しかし、それはC#ではうまくいかないようです。アナログはありますか?
あなたがそれをしたいと思ういくつかの標準的な状況があります:
int x = sizeof(T) // where T is a generic type
残念ながら機能しません:-)
int x = Marshal.SizeOf(T) // where T is a generic type
charand bool( Marshal.SizeOf(typeof(char))== 2 の代わりに 1、== 1 の代わりにMarshal.SizeOf(typeof(bool))4)を除いて機能します。
int x = sizeof(IntPtr);
動作しませんが、次のように実行できます
int x = Marshal.SizeOf(typeof(IntPtr));
または、より良い
int x = IntPtr.Size;
他のすべての基本型 ( byte、sbyte、short、ushort、int、uint、long、ulong、float、 )doubleは固定長であるため、実行でき、decimal常に4 になります。boolcharsizeof(int)