Wat*_* v2 1 c# clr language-features language-design
1)什么是int?它有什么不同struct System.Int32吗?我知道前者是CLR类型的C#别名(typedef或#define等价物)System.Int32.这种理解是否正确?
2)当我们说:
IComparable x = 10;
Run Code Online (Sandbox Code Playgroud)
这就像说:
IComparable x = new System.Int32();
Run Code Online (Sandbox Code Playgroud)
但我们不能new一个结构,对吗?
或者在C语法中:
struct System.In32 *x;
x=>someThing = 10;
Run Code Online (Sandbox Code Playgroud)
3)什么是String一个大写的小号?我在Reflector中看到它是sealed String类,当然,它是一个引用类型,与System.Int32上面不同,它是一个值类型.
string然而,什么是非资本化的?这也是这个类的C#别名吗?
为什么我在Reflector中看不到别名定义?
4)如果你愿意,试着跟着我这个微妙的思路.我们知道特定类型的存储位置只能访问其接口上的属性和成员.这意味着:
Person p = new Customer();
p.Name = "Water Cooler v2"; // legal because as Name is defined on Person.
Run Code Online (Sandbox Code Playgroud)
但
// illegal without an explicit cast even though the backing
// store is a Customer, the storage location is of type
// Person, which doesn't support the member/method being
// accessed/called.
p.GetTotalValueOfOrdersMade();
Run Code Online (Sandbox Code Playgroud)
现在,通过该推断,请考虑以下情形:
int i = 10;
// obvious System.object defines no member to
// store an integer value or any other value in.
// So, my question really is, when the integer is
// boxed, what is the *type* it is actually boxed to.
// In other words, what is the type that forms the
// backing store on the heap, for this operation?
object x = i;
Run Code Online (Sandbox Code Playgroud)
更新
感谢您的回答,Eric Gunnerson和Aaronought.我担心我无法很好地表达我的问题以吸引非常令人满意的答案.麻烦的是,我确实知道表面问题的答案,而且我绝不是新手程序员.
但我不得不承认,只要我是程序员,即使我编写了正确的代码,对于语言及其底层平台/运行时如何处理类型存储的复杂性的深刻理解也一直困扰着我.
int是别名System.Int32.类型是相同的.
IComparable x = 10与写作类似var i = 10; IComparable x = i.编译器选择它认为最常用的常量类型,然后进行隐式转换IComparable.
string是System.String#1 的别名,类似于#1.您无法在Reflector中看到别名定义,因为别名是C#编译器的一部分,而不是.NET Framework本身.(例如,它在VB.NET中有所不同.)
盒装整数或其他值类型是对值的引用.您可能会将其视为附加了某些类型信息的指针.然而,实际的支持类型很简单System.Object.
| 归档时间: |
|
| 查看次数: |
297 次 |
| 最近记录: |