gpt4 book ai didi

c# - 几个 C# 语言问题

转载 作者:太空宇宙 更新时间:2023-11-03 18:18:17 24 4
gpt4 key购买 nike

1) 什么是int ?它与 struct 有什么不同吗? System.Int32 ?我知道前者是 CLR 类型 typedef 的 C# 别名(#defineSystem.Int32 等价物) .这种理解正确吗?

2)当我们说:

IComparable x = 10;

是不是就像在说:
IComparable x = new System.Int32();

但是我们不能 new一个结构,对吧?

或类似 C 的语法:
struct System.In32 *x;
x=>someThing = 10;

3) 什么是 String用大写的 S?我在 Reflector 中看到它是 sealed String类,当然,它是一个引用类型,不像 System.Int32上面,这是一个值类型。

什么是 string , 但是没有大写的 s?这也是这个类的 C# 别名吗?

为什么我在 Reflector 中看不到别名定义?

4)如果你愿意,试着跟着我走这条微妙的思路。我们知道,特定类型的存储位置只能访问其接口(interface)上的属性和成员。这意味着:
Person p = new Customer();
p.Name = "Water Cooler v2"; // legal because as Name is defined on Person.


// 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();

现在,根据这个推论,考虑这种情况:
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;

更新

感谢您的回答,Eric Gunnerson 和 Aaronought。恐怕我无法很好地表达我的问题以吸引非常令人满意的答案。问题是,表面上我确实知道我的问题的答案,而且我绝不是新手程序员。

但我必须承认,只要我是一名程序员,即使我编写了正确的代码,对语言及其底层平台/运行时处理类型存储的复杂性的更深入理解也一直没有得到解决。

最佳答案

  • intSystem.Int32 的别名.类型是相同的。
  • IComparable x = 10类似于写 var i = 10; IComparable x = i .编译器选择它认为最可能的常量类型,然后隐式转换为 IComparable .
  • stringSystem.String 的别名,类似于#1。您在 Reflector 中看不到别名定义,因为别名是 C# 编译器的一部分,而不是 .NET Framework 本身。 (例如,在 VB.NET 中就不同了。)
  • 装箱整数或其他值类型是对该值的引用。您可能会认为它是一个带有一些类型信息的指针。然而,实际的支持类型只是 System.Object .
  • 关于c# - 几个 C# 语言问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2878596/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com