暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

C#中的int、long、float、double等类型都占多少个字节的内存?

DotNet开发跳槽 2021-07-22
792

这个问题还真不知道,直接上测试代码,一目了然。

    using System;


    public static class Program


    {


    public static void Main(string[] args)


    {


    Console.WriteLine("{0}: {1} byte(s) scope:[{2}-{3}]",


    typeof(byte).Name.PadLeft(8), sizeof(byte).NumberPad(2),


    byte.MinValue.NumberPad(32, true), byte.MaxValue.NumberPad(32));


    Console.WriteLine("{0}: {1} byte(s) scope:[{2}-{3}]",


    typeof(sbyte).Name.PadLeft(8), sizeof(sbyte).NumberPad(2),


    sbyte.MinValue.NumberPad(32, true), sbyte.MaxValue.NumberPad(32));


    Console.WriteLine("{0}: {1} byte(s) scope:[{2}-{3}]",


    typeof(short).Name.PadLeft(8), sizeof(short).NumberPad(2),


    short.MinValue.NumberPad(32, true), short.MaxValue.NumberPad(32));


    Console.WriteLine("{0}: {1} byte(s) scope:[{2}-{3}]",


    typeof(ushort).Name.PadLeft(8), sizeof(ushort).NumberPad(2),


    ushort.MinValue.NumberPad(32, true), ushort.MaxValue.NumberPad(32));


    Console.WriteLine("{0}: {1} byte(s) scope:[{2}-{3}]",


    typeof(int).Name.PadLeft(8), sizeof(int).NumberPad(2),


    int.MinValue.NumberPad(32, true), int.MaxValue.NumberPad(32));


    Console.WriteLine("{0}: {1} byte(s) scope:[{2}-{3}]",


    typeof(uint).Name.PadLeft(8), sizeof(uint).NumberPad(2),


    uint.MinValue.NumberPad(32, true), uint.MaxValue.NumberPad(32));


    Console.WriteLine("{0}: {1} byte(s) scope:[{2}-{3}]",


    typeof(long).Name.PadLeft(8), sizeof(long).NumberPad(2),


    long.MinValue.NumberPad(32, true), long.MaxValue.NumberPad(32));


    Console.WriteLine("{0}: {1} byte(s) scope:[{2}-{3}]",


    typeof(ulong).Name.PadLeft(8), sizeof(ulong).NumberPad(2),


    ulong.MinValue.NumberPad(32, true), ulong.MaxValue.NumberPad(32));


    Console.WriteLine("{0}: {1} byte(s) scope:[{2}-{3}]",


    typeof(float).Name.PadLeft(8), sizeof(float).NumberPad(2),


    float.MinValue.NumberPad(32, true), float.MaxValue.NumberPad(32));


    Console.WriteLine("{0}: {1} byte(s) scope:[{2}-{3}]",


    typeof(double).Name.PadLeft(8), sizeof(double).NumberPad(2),


    double.MinValue.NumberPad(32, true), double.MaxValue.NumberPad(32));


    Console.WriteLine("{0}: {1} byte(s) scope:[{2}-{3}]",


    typeof(decimal).Name.PadLeft(8), sizeof(decimal).NumberPad(2),


    decimal.MinValue.NumberPad(32, true), decimal.MaxValue.NumberPad(32));


    Console.WriteLine("{0}: {1} byte(s)",


    typeof(bool).Name.PadLeft(8), sizeof(bool).NumberPad(2));


    Console.WriteLine("{0}: {1} byte(s)",


    typeof(char).Name.PadLeft(8), sizeof(char).NumberPad(2));


    Console.WriteLine("{0}: {1} byte(s) ",


    typeof(IntPtr).Name.PadLeft(8), IntPtr.Size.NumberPad(2));


    Console.ReadLine();


    }


    public static string NumberPad<T>(this T value, int length, bool right = false)


    {


    if (right)


    {


    return value.ToString().PadRight(length);


    }


    else


    {


    return value.ToString().PadLeft(length);


    }


    }


    }

    结果如下:

    出处:https://www.tnblog.net/ylanks/article/details/6236


    版权申明:本文来源于网友收集或网友提供,仅供学习交流之用,如果有侵权,请转告版主或者留言,本公众号立即删除。


    支持小微:

    腾讯云 搞活动了?玩服务器的可以搞搞。就这几天时间。

    轻量  1C2G6M 60GB SSD盘297元/3年

    链接:https://curl.qcloud.com/bR8ycXZa



    右下角,您点一下在看图片

    小微工资涨1毛

    商务合作QQ:185601686



    文章转载自DotNet开发跳槽,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

    评论