32位windows下各数据类型在内存中占用的字节数

来源:互联网 发布:origin矩阵画图 编辑:程序博客网 时间:2024/06/06 07:08

测试系统:Windows2003 server

C#中测试的结果:

int:4bytes;
Int16:2bytes;
Int32:4bytes;
Int64:8bytes;
byte:1bytes;
bool:1bytes;
Single:4bytes;
decimal:16bytes;
double:8bytes;
char:2bytes;
DateTime:8bytes;
float:4bytes;
Guid:16bytes;
long:8bytes;
UIntPtr:4bytes;
uint:4bytes;
UInt16:2bytes;
UInt32:4bytes;
UInt64:8bytes;
ulong:8bytes;

C#测试的代码:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
    struct display
    {
        public string paramType;
        public int size;
    }
    class Program
    {
       
        static void Main(string[] args)
        {
            unsafe
            {


                display[] sizes = new display[20];
                sizes[0].size = sizeof(int);
                sizes[0].paramType = "int";

                sizes[1].size = sizeof(Int16);
                sizes[1].paramType = "Int16";

                sizes[2].size = sizeof(Int32);
                sizes[2].paramType = "Int32";

                sizes[3].size = sizeof(Int64);
                sizes[3].paramType = "Int64";

                sizes[4].size = sizeof(byte);
                sizes[4].paramType = "byte";

                sizes[5].size = sizeof(bool);
                sizes[5].paramType = "bool";

                sizes[6].size = sizeof(Single);
                sizes[6].paramType = "Single";

                sizes[7].size = sizeof(decimal);
                sizes[7].paramType = "decimal";

                sizes[8].size = sizeof(double);
                sizes[8].paramType = "double";

                sizes[9].size = sizeof(char);
                sizes[9].paramType = "char";

                sizes[10].size = sizeof(DateTime);
                sizes[10].paramType = "DateTime";

                sizes[11].size = sizeof(float);
                sizes[11].paramType = "float";

                sizes[12].size = sizeof(Guid);
                sizes[12].paramType = "Guid";

                sizes[13].size = sizeof(long);
                sizes[13].paramType = "long";

                sizes[14].size = sizeof(UIntPtr);
                sizes[14].paramType = "UIntPtr";

                sizes[15].size = sizeof(uint);
                sizes[15].paramType = "uint";

                sizes[16].size = sizeof(UInt16);
                sizes[16].paramType = "UInt16";

                sizes[17].size = sizeof(UInt32);
                sizes[17].paramType = "UInt32";

                sizes[18].size = sizeof(UInt64);
                sizes[18].paramType = "UInt64";

                sizes[19].size = sizeof(ulong);
                sizes[19].paramType = "ulong";

 

                StreamWriter sw = new StreamWriter(@"D:/1.txt", true, Encoding.UTF8);
                sw.WriteLine("========================================================================");
                sw.WriteLine("                              size of each type                         ");
                sw.WriteLine("========================================================================");
                foreach (display temp in sizes)
                {
                    if (temp.paramType != null)
                        sw.WriteLine(temp.paramType + ":" + temp.size.ToString() + "bytes;");


                }
                bool booltest = true;
                sw.WriteLine("bool is true:"+booltest.ToString());
                booltest = false;
                sw.WriteLine("bool is false:" + booltest.ToString());
                sw.Close();

                Console.Read();

            }
        }
    }
}
 

原创粉丝点击