上测试代码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),...
上测试代码
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);
}
}
}
结果如下
Byte: 1 byte(s) scope:[0 - 255]
SByte: 1 byte(s) scope:[-128 - 127]
Int16: 2 byte(s) scope:[-32768 - 32767]
UInt16: 2 byte(s) scope:[0 - 65535]
Int32: 4 byte(s) scope:[-2147483648 - 2147483647]
UInt32: 4 byte(s) scope:[0 - 4294967295]
Int64: 8 byte(s) scope:[-9223372036854775808 - 9223372036854775807]
UInt64: 8 byte(s) scope:[0 - 18446744073709551615]
Single: 4 byte(s) scope:[-3.4028235E+38 - 3.4028235E+38]
Double: 8 byte(s) scope:[-1.7976931348623157E+308 - 1.7976931348623157E+308]
Decimal: 16 byte(s) scope:[-79228162514264337593543950335 - 79228162514264337593543950335]
Boolean: 1 byte(s)
Char: 2 byte(s)
IntPtr: 8 byte(s)
以上结果需要注意,在32位系统中,IntPtr为4字节,在64位系统中,IntPtr为8字节。
织梦狗教程
本文标题为:C#中的int、long、float、double等类型都占多少个字节的内存?
基础教程推荐
猜你喜欢
- c# 免费组件html转pdf的实现过程 2023-06-14
- C# 实现PPT 每一页转成图片过程解析 2023-02-02
- C#编程中常见数据结构的比较(Unity3D游戏开发) 2023-01-22
- .Net Core 在 Linux-Centos上的部署实战教程(二) 2023-09-28
- C#实现从位图到布隆过滤器的方法 2023-06-21
- 浅析C# 中的类型系统(值类型和引用类型) 2022-10-28
- C#实现同步模式下的端口映射程序 2023-06-15
- C#中的SQL查询(Linq) 2023-11-09
- C#中Linq的去重方式Distinct详解 2023-06-21
- Windows 10 UWP – C#:如何检查网络类型(EDGE / 3G / LTE),不仅仅是蜂窝网络与WLAN? 2023-09-18
