此MSDN文章处理数据类型。
它说:
对于Java中的每种原始数据类型,核心类库都提供了一个包装器类,将其表示为Java对象。例如,Int32类包装了int数据类型,而Double类包装了double数据类型。
另一方面,C#中的所有原始数据类型都是System名称空间中的对象。对于每种数据类型,都提供了一个简短的名称或别名。例如,int是System.Int32的缩写,而double是System.Double的缩写。
我运行了Mono代码以读取System.Int32 struct的实现。
我发现了几行迫使我问这个问题:
public const int MaxValue = 0x7fffffff;
public const int MinValue = -2147483648;
// This field is looked up by name in the runtime
internal int m_value;
我假设MS将以相同的方式实现该结构。这和包装器有什么不同吗?文件试图传达什么?
如果可以相信MSDN,那么System.Int32结构将是无限递归的,至少对我来说是极大的困惑。
这和包装器有什么不同吗?
有没有包装类型的基本类型在C#中好像有在Java和别名int
为System.Int32
不喜欢的包装东西。它只是System.Int32
一个不同的名字。
C#中的原始类型可以像Java中那样被装箱,但是在C#中被装箱时它们被装箱,System.Object
而在Java中被装箱到它们的包装器类型。
文件试图传达什么?
这两种相似但不同的语言之间是有区别的。乍一看,这些东西看起来是一样的:
Java:int
->Integer
C#:int
->System.Int32
但是,事实并非如此。在Java中,它int
是一种原始类型,并且Integer
是一个类,int
当int
需要装箱s时,它充当包装器。在C#中,int
和System.Int32
是完全相同的东西 (原始类型)。
如果可以相信MSDN,那么System.Int32结构将是无限递归的,至少对我来说是极大的困惑。
System.Int32
和int
是恰好在C#同一类型; int
仅仅是的便捷别名System.Int32
。在Java中,int
和Integer
是不同的。在C#中,int
并且System.Int32
是没有不同的。
因此,非常清楚:
public const int MaxValue = 0x7fffffff;
public const int MinValue = -2147483648;
// This field is looked up by name in the runtime
internal int m_value;
可以重写为
public const System.Int32 MaxValue = 0x7fffffff;
public const System.Int32 MinValue = -2147483648;
// This field is looked up by name in the runtime
internal System.Int32 m_value;
这根本不会改变含义,因为int
和System.Int32
是完全相同的东西。然后,对于内置类型的特殊处理,您应该参考上一个问题。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句