无法隐式转换类型“int”到“短”
我编写了以下小程序来打印斐波那契数列:
static void Main(string[] args)
{
Console.Write("Please give a value for n:");
Int16 n = Int16.Parse(Console.ReadLine());
Int16 firstNo = 0;
Int16 secondNo = 1;
Console.WriteLine(firstNo);
Console.WriteLine(secondNo);
for (Int16 i = 0; i < n; i++)
{
//Problem on this line
Int16 answer = firstNo + secondNo;
Console.WriteLine(answer);
firstNo = secondNo;
secondNo = answer;
}
Console.ReadLine();
}
编译消息是:
无法隐式转换类型“int” 到“短”。显式转换 存在(你缺少演员吗?)
既然涉及的所有内容都是 Int16 (短),那么为什么会发生任何隐式转换?更具体地说,为什么这里失败(而不是在最初为变量分配 int 时失败)?
如果有解释,我们将不胜感激。
I wrote the following small program to print out the Fibonacci sequence:
static void Main(string[] args)
{
Console.Write("Please give a value for n:");
Int16 n = Int16.Parse(Console.ReadLine());
Int16 firstNo = 0;
Int16 secondNo = 1;
Console.WriteLine(firstNo);
Console.WriteLine(secondNo);
for (Int16 i = 0; i < n; i++)
{
//Problem on this line
Int16 answer = firstNo + secondNo;
Console.WriteLine(answer);
firstNo = secondNo;
secondNo = answer;
}
Console.ReadLine();
}
The compilation message is:
Cannot implicitly convert type 'int'
to 'short'. An explicit conversion
exists (are you missing a cast?)
Since everything involved is an Int16 (short) then why are there any implicit conversions going on? And more specificially why the failure here (and not when initially assigning an int to the variable)?
An explanation would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
在执行 add 函数时,Microsoft 会将您的
Int16
变量转换为Int32
。将以下内容更改
为...
Microsoft converts your
Int16
variables intoInt32
when doing the add function.Change the following:
into...
阅读 Eric Lippert 对这些问题的回答
Read Eric Lippert 's answers to these questions
将两个
Int16
值相加会产生一个Int32
值。您必须将其转换为Int16
:您可以通过将所有数字切换为
Int32
来避免此问题。Adding two
Int16
values result in anInt32
value. You will have to cast it toInt16
:You can avoid this problem by switching all your numbers to
Int32
.两个
Int16
变量相加的结果是一个Int32
:它输出
Int32
。The result of summing two
Int16
variables is anInt32
:It outputs
Int32
.问题是,正如其他人已经指出的那样,添加两个
Int16
会产生Int32
。您的第二个问题,为什么在声明这两个变量时尚未出现此问题,解释如下:http://msdn.microsoft.com/en-us/library/ybs77ex4%28v=VS.71%29.aspx:
因此,它在您的声明中起作用的原因很简单,即已知所提供的文字适合
short
。The problem is, that adding two
Int16
results in anInt32
as others have already pointed out.Your second question, why this problem doesn't already occur at the declaration of those two variables is explained here: http://msdn.microsoft.com/en-us/library/ybs77ex4%28v=VS.71%29.aspx:
So, the reason why it works in your declaration is simply that the literals provided are known to fit into a
short
.加号运算符首先将操作数转换为 int,然后再进行加法。所以结果是int。您需要显式地将其转换回短类型,因为从“较长”类型到“较短”类型的转换是显式的,这样您就不会因隐式转换而意外丢失数据。
至于为什么 int16 被强制转换为 int,答案是,因为这是 C# 规范。 C# 之所以如此,是因为它的设计与 CLR 的工作方式紧密匹配,而 CLR 只有 32/64 位算术,而不是 16 位。 CLR 之上的其他语言可能会选择以不同的方式公开这一点。
The plus operator converts operands to int first and then does the addition. So the result is the int. You need to cast it back to short explicitly because conversions from a "longer" type to "shorter" type a made explicit, so that you don't loose data accidentally with an implicit cast.
As to why int16 is cast to int, the answer is, because this is what is defined in C# spec. And C# is this way is because it was designed to closely match to the way how CLR works, and CLR has only 32/64 bit arithmetic and not 16 bit. Other languages on top of CLR may choose to expose this differently.
由于某些奇怪的原因,您可以使用 += 运算符来添加短裤。
For some strange reason, you can use the += operator to add shorts.
该行
被解释为
Simply,因为不存在 Int16 算术之类的东西。
简单的解决方案:不要使用 Int16。使用 Int32 或简单地使用
int
。int
是默认的整数类型。短和长仅在特殊情况下使用。The line
is interpreted as
Simply because there is no such thing as Int16 arithmetic.
The simple solution: Do not use Int16. Use Int32 or simply
int
.int
is your default integer type. short and long are only used in special cases.这是因为两个
Int16
相加的结果是一个Int32
。检查此处的“转换”段落:http:// /msdn.microsoft.com/en-us/library/ybs77ex4%28v=vs.71%29.aspx
That's because the result of adding two
Int16
is anInt32
.Check the "conversions" paragraph here: http://msdn.microsoft.com/en-us/library/ybs77ex4%28v=vs.71%29.aspx