【问题标题】:Why does `Int32` use `int` in its source code? [duplicate]为什么`Int32`在其源代码中使用`int`? [复制]
【发布时间】:2015-06-08 10:53:31
【问题描述】:

为什么Int32 在其source code 中使用intInt32 在 C# 中不等于 int 吗?那么int的原始源代码是什么?我很困惑..

[Serializable]
[System.Runtime.InteropServices.StructLayout(LayoutKind.Sequential)] 
[System.Runtime.InteropServices.ComVisible(true)]
#if GENERICS_WORK
    public struct Int32 : IComparable, IFormattable, IConvertible
        , IComparable<Int32>, IEquatable<Int32>
///     , IArithmetic<Int32>
#else
    public struct Int32 : IComparable, IFormattable, IConvertible
#endif
    {
        internal int m_value; // Here????

        public const int MaxValue = 0x7fffffff;
        public const int MinValue = unchecked((int)0x80000000);

        ...
    }

Booleanbool 之间也存在同样的问题。

【问题讨论】:

  • Int32 是struct。它包含用于操作 int 的方法和内容,int 是系统定义的数据类型。
  • int的原始源代码。一些实际的内部工作是编译器和运行时的一部分,但只要有源代码,那就是源代码。
  • 为什么你认为它不必使用int?
  • @TimSchmelter 绝对重复。很好的发现。 :)

标签: c# int32 reference-source


【解决方案1】:

intbool 等分别是 Int32Boolean 等内置类型的简单别名。它们是速记,所以使用它们是有意义的。甚至在他们自己的定义中。

别名内置在编译器中,因此在访问int 之前,不必编译Int32 的“鸡与蛋”情况。 int 没有“原始源代码”。 “源”用于Int32,并在内置于 CLR(用于处理原语)和框架(用于定义操作这些原语的功能)中的功能之间共享。作为this answer to a duplicate question explainsInt32 的来源不是类型本身的有效定义;这是内置在 CLR 中的。因此,编译器必须在该文件中伪造int 的正常非法使用,以允许Int32 类型具有功能。

【讨论】:

  • 所以 Int32 “递归”地“包含”自身?
  • 这根本不能回答问题。
  • @hvd,我想你很困惑。也许尽管您愿意解释为什么它不能回答问题,甚至自己“正确”地回答问题?
  • @codroipo,没有 intSystem.Int32 的编译器关键字别名。
  • @DavidArno 在我发表评论后,您对您的答案进行了重大修改(可以从评论时间和编辑时间进行验证)。你现在确实回答了这个问题。在链接的问题中不如 Eric Lippert,但比以前好得多。
猜你喜欢
  • 2021-07-16
  • 2011-07-28
  • 2023-04-05
  • 2017-04-20
  • 2011-06-04
  • 1970-01-01
  • 2015-06-30
  • 2014-04-21
  • 1970-01-01
相关资源
最近更新 更多