tnblog
首页
视频
资源
登录
没有个性,不需要签名
排名
4
文章
473
粉丝
3
评论
2
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术

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

6831人阅读 2021/7/21 15:38 总访问:11213 评论:2 收藏:0 手机
分类: dotnet

上测试代码

  1. using System;
  2. public static class Program
  3. {
  4. public static void Main(string[] args)
  5. {
  6. Console.WriteLine("{0}: {1} byte(s) scope:[{2}-{3}]",
  7. typeof(byte).Name.PadLeft(8), sizeof(byte).NumberPad(2),
  8. byte.MinValue.NumberPad(32, true), byte.MaxValue.NumberPad(32));
  9. Console.WriteLine("{0}: {1} byte(s) scope:[{2}-{3}]",
  10. typeof(sbyte).Name.PadLeft(8), sizeof(sbyte).NumberPad(2),
  11. sbyte.MinValue.NumberPad(32, true), sbyte.MaxValue.NumberPad(32));
  12. Console.WriteLine("{0}: {1} byte(s) scope:[{2}-{3}]",
  13. typeof(short).Name.PadLeft(8), sizeof(short).NumberPad(2),
  14. short.MinValue.NumberPad(32, true), short.MaxValue.NumberPad(32));
  15. Console.WriteLine("{0}: {1} byte(s) scope:[{2}-{3}]",
  16. typeof(ushort).Name.PadLeft(8), sizeof(ushort).NumberPad(2),
  17. ushort.MinValue.NumberPad(32, true), ushort.MaxValue.NumberPad(32));
  18. Console.WriteLine("{0}: {1} byte(s) scope:[{2}-{3}]",
  19. typeof(int).Name.PadLeft(8), sizeof(int).NumberPad(2),
  20. int.MinValue.NumberPad(32, true), int.MaxValue.NumberPad(32));
  21. Console.WriteLine("{0}: {1} byte(s) scope:[{2}-{3}]",
  22. typeof(uint).Name.PadLeft(8), sizeof(uint).NumberPad(2),
  23. uint.MinValue.NumberPad(32, true), uint.MaxValue.NumberPad(32));
  24. Console.WriteLine("{0}: {1} byte(s) scope:[{2}-{3}]",
  25. typeof(long).Name.PadLeft(8), sizeof(long).NumberPad(2),
  26. long.MinValue.NumberPad(32, true), long.MaxValue.NumberPad(32));
  27. Console.WriteLine("{0}: {1} byte(s) scope:[{2}-{3}]",
  28. typeof(ulong).Name.PadLeft(8), sizeof(ulong).NumberPad(2),
  29. ulong.MinValue.NumberPad(32, true), ulong.MaxValue.NumberPad(32));
  30. Console.WriteLine("{0}: {1} byte(s) scope:[{2}-{3}]",
  31. typeof(float).Name.PadLeft(8), sizeof(float).NumberPad(2),
  32. float.MinValue.NumberPad(32, true), float.MaxValue.NumberPad(32));
  33. Console.WriteLine("{0}: {1} byte(s) scope:[{2}-{3}]",
  34. typeof(double).Name.PadLeft(8), sizeof(double).NumberPad(2),
  35. double.MinValue.NumberPad(32, true), double.MaxValue.NumberPad(32));
  36. Console.WriteLine("{0}: {1} byte(s) scope:[{2}-{3}]",
  37. typeof(decimal).Name.PadLeft(8), sizeof(decimal).NumberPad(2),
  38. decimal.MinValue.NumberPad(32, true), decimal.MaxValue.NumberPad(32));
  39. Console.WriteLine("{0}: {1} byte(s)",
  40. typeof(bool).Name.PadLeft(8), sizeof(bool).NumberPad(2));
  41. Console.WriteLine("{0}: {1} byte(s)",
  42. typeof(char).Name.PadLeft(8), sizeof(char).NumberPad(2));
  43. Console.WriteLine("{0}: {1} byte(s) ",
  44. typeof(IntPtr).Name.PadLeft(8), IntPtr.Size.NumberPad(2));
  45. Console.ReadLine();
  46. }
  47. public static string NumberPad<T>(this T value, int length, bool right = false)
  48. {
  49. if (right)
  50. {
  51. return value.ToString().PadRight(length);
  52. }
  53. else
  54. {
  55. return value.ToString().PadLeft(length);
  56. }
  57. }
  58. }

结果如下

  1. Byte: 1 byte(s) scope:[0 - 255]
  2. SByte: 1 byte(s) scope:[-128 - 127]
  3. Int16: 2 byte(s) scope:[-32768 - 32767]
  4. UInt16: 2 byte(s) scope:[0 - 65535]
  5. Int32: 4 byte(s) scope:[-2147483648 - 2147483647]
  6. UInt32: 4 byte(s) scope:[0 - 4294967295]
  7. Int64: 8 byte(s) scope:[-9223372036854775808 - 9223372036854775807]
  8. UInt64: 8 byte(s) scope:[0 - 18446744073709551615]
  9. Single: 4 byte(s) scope:[-3.4028235E+38 - 3.4028235E+38]
  10. Double: 8 byte(s) scope:[-1.7976931348623157E+308 - 1.7976931348623157E+308]
  11. Decimal: 16 byte(s) scope:[-79228162514264337593543950335 - 79228162514264337593543950335]
  12. Boolean: 1 byte(s)
  13. Char: 2 byte(s)
  14. IntPtr: 8 byte(s)
评价