在C#中,为什么" INT"为System.Int32的别名? [英] In C#, why is "int" an alias for System.Int32?

查看:297
本文介绍了在C#中,为什么" INT"为System.Int32的别名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于C#支持 INT8 的Int16 的Int32 的Int64 ,为什么语言的设计者选择定义 INT 的别名INT32 ,而不是允许它根据什么原生架构认为是一个

Since C# supports Int8, Int16, Int32 and Int64, why did the designers of the language choose to define int as an alias for Int32 instead of allowing it to vary depending on what the native architecture considers to be a word?

我还没有为 INT 任何具体需求表现比它,我只是问纯粹出于兴趣广博的方式不同。

I have not had any specific need for int to behave differently than the way it does, I am only asking out of pure encyclopedic interest.

我想一个64位RISC架构可以想见,存在这将最有效地只支持64位值,并在其中的32位数量操作将需要额外的操作。这样的架构将在其中的程序坚持使用32位整数,这是说C#的另一种世界的一个缺点,成为未来所有的语言,从根本上防止硬件设计人员从不断想出这样的在未来的架构。

I would think that a 64-bit RISC architecture could conceivably exist which would most efficiently support only 64-bit quantities, and in which manipulations of 32-bit quantities would require extra operations. Such an architecture would be at a disadvantage in a world in which programs insist on using 32-bit integers, which is another way of saying that C#, becoming the language of the future and all, essentially prevents hardware designers from ever coming up with such an architecture in the future.

计算器不鼓励投机的答案,所以请,如果你的信息来自可靠来源唯一的答案。我已经注意到了所以一些成员是微软内部人士透露,所以我希望他们也许能启发我们对这个问题

StackOverflow does not encourage speculating answers, so please answer only if your information comes from a dependable source. I have noticed that some members of SO are Microsoft insiders, so I was hoping that they might be able to enlighten us on this subject.

<子>注1:I并实际上阅读所有的答案和的这样:它是安全的假设一个int总是会在C#32位,但没有发现任何提示,在为什么的,我在此要求? 。问题

Note 1: I did in fact read all answers and all comments of SO: Is it safe to assume an int will always be 32 bits in C#? but did not find any hint as to the why that I am asking in this question.

<子>注2:这个问题的可行性上是SO(不了了之了)这里讨论:的Meta:我能问一个为什么他们做这种方式类型的问题?

推荐答案

我相信,他们的主要理由是靶向CLR程序的可移植性。如果他们允许类型基本为 INT 是依赖于平台的,因此移植的程序为CLR会成为一个很大的难度。增殖的typedef 在平台中立Ç-ed整型/ C ++代码覆盖使用内置的 INT 是一个间接的暗示,为什么CLR的设计者决定让内建类型平台无关的。差异一样,是一个很大的抑制剂基于虚拟机执行系统的一次编写,到处运行的目标。

I believe that their main reason was portability of programs targeting CLR. If they were to allow a type as basic as int to be platform-dependent, making portable programs for CLR would become a lot more difficult. Proliferation of typedef-ed integral types in platform-neutral C/C++ code to cover the use of built-in int is an indirect hint as to why the designers of CLR decided on making built-in types platform-independent. Discrepancies like that are a big inhibitor to the "write once, run anywhere" goal of execution systems based on VMs.

修改更多往往比不,大小的 INT 播放到你的代码隐含通过位运算,而不是通过算术(毕竟,有什么可能出问题的我++ ,对吧?),但错误通常是更加微妙。考虑下面一个例子:

Edit More often than not, the size of an int plays into your code implicitly through bit operations, rather than through arithmetics (after all, what could possibly go wrong with the i++, right?) But the errors are usually more subtle. Consider an example below:

const int MaxItem = 20;
var item = new MyItem[MaxItem];
for (int mask = 1 ; mask != (1<<MaxItem) ; mask++) {
    var combination = new HashSet<MyItem>();
    for (int i = 0 ; i != MaxItem ; i++) {
        if ((mask & (1<<i)) != 0) {
            combination.Add(item[i]);
        }
    }
    ProcessCombination(combination);
}

这代码计算和处理的20项所有组合。正如你所知道的,代码惨遭失败,16位 INT 的系统上,但工作正常的32或64位整数。

This code computes and processes all combinations of 20 items. As you can tell, the code fails miserably on a system with 16-bit int, but works fine with ints of 32 or 64 bits.

不安全代码将提供头痛的另一个来源:当 INT 被固定在某一大小(例如,32)代码分配的4倍的数量字节作为它需要名帅将工作整数的数量,即使它是技术上不正确的地方的使用4的sizeof(INT)。而且,这种技术上不正确的代码将保持便携!

Unsafe code would provide another source of headache: when the int is fixed at some size (say, 32) code that allocates 4 times the number of bytes as the number of ints that it needs to marshal would work, even though it is technically incorrect to use 4 in place of sizeof(int). Moreover, this technically incorrect code would remain portable!

最后,这样的小东西在很大程度上发挥到平台的看法是好或坏。 .NET程序的用户并不关心一个程序崩溃,因为它的程序员由非便携的错误,或者是CLR车。这类似于早期窗户被广泛视为非稳定由于司机的质量差的方式。对于大多数用户来说,崩溃只是另一种.NET程序崩溃,而不是一个程序员的问题。因此是良好的生态系统.NET,使标准的宽容尽可能的看法。

Ultimately, small things like that play heavily into the perception of platform as "good" or "bad". Users of .NET programs do not care that a program crashes because its programmer made a non-portable mistake, or the CLR is buggy. This is similar to the way the early Windows were widely perceived as non-stable due to poor quality of drivers. To most users, a crash is just another .NET program crash, not a programmers' issue. Therefore is is good for perception of the ".NET ecosystem" to make the standard as forgiving as possible.

这篇关于在C#中,为什么&QUOT; INT&QUOT;为System.Int32的别名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆