c# - c#中的int是什么?

标签 c# value-type reference-type

C# 中的 int 是什么?它是关键字,还是派生自 system.ValueTypes 的类?如果它是一个关键字那么下面几行如何编译

int i = new int(); // If int is not a class then how does it have a default constructor

Console.WriteLine(i.ToString()); // If int is not a class then how does it have member functions

如果 int 是一个类,那么为什么不需要总是用 new 初始化它?下面一行如何编译?

int i = 8;

最佳答案

int 是 CTS 类型 System.Int32别名 structure .

阅读 Eric Lippert 发布的 SO 答案 - How do ValueTypes derive from Object (ReferenceType) and still be ValueTypes?

关于c# - c#中的int是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11589354/

相关文章:

c# - 如何使用 Texture2D.FromStream 加载透明 PNG

swift - 在非类绑定(bind)协议(protocol)的扩展中,实例必须被视为值类型

reference - 我什么时候使用浅拷贝?

c++ - 为什么 `std::remove_const` 在与 `const` 一起使用时不删除引用对象的 `decltype` -ness?

c#-4.0 - 为什么当引用更改时,可变 StringBuilder 的行为类似于不可变字符串?

C# react 扩展当 OnNext 花费很长时间并且可观察到产生新事件时会发生什么

c# - 使用反射传递参数时,Visual Studio 转换是多余的

c# - 将负小数转换为字符串会丢失 -

ios - Swift:移动后恢复 UIView 中心

c# - 为什么 Nullable<T> 不匹配作为泛型约束的引用类型