首页 文章详情

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

llovebo | 478 2021-07-22 13:29 0 0 0
UniSMS (合一短信)

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

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);
}
}
}

结果如下:

c8262aa406a349689cd0227538ec7a9f.webp

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


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


支持小微:

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

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

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



右下角,您点一下在看图片d531b31556778369999ece36fb82927f.webp

小微工资涨1毛

商务合作QQ:185601686


1b0bbcc26514f712049bc275858f433d.webp


good-icon 0
favorite-icon 0
收藏
回复数量: 0
    暂无评论~~
    Ctrl+Enter