上测试代码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# 字符串操作总结 2023-02-25
- C#实现Check Password和锁定输错密码锁定账户功能 2023-02-07
- C# 遍历datatable字段名和value的案例 2023-03-29
- C#写差异文件备份工具的示例 2023-03-14
- C# 解决datagridview控件显示大量数据拖拉卡顿问题 2023-03-28
- c# – 使用实体框架将更改保存回数据库 2023-11-23
- C#如何利用反射将枚举绑定到下拉框详解 2022-12-26
- C#中使用ArrayPool和MemoryPool实例 2023-04-21
- 如何实现定时推送的具体方案 2023-04-14
- Winform界面中实现通用工具栏按钮的事件处理方法 2022-11-24