无法隐式转换类型“int”到“短”

发布于 2024-11-05 07:30:15 字数 756 浏览 4 评论 0原文

我编写了以下小程序来打印斐波那契数列:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

琴流音 2024-11-12 07:30:15

在执行 add 函数时,Microsoft 会将您的 Int16 变量转换为 Int32

将以下内容更改

Int16 answer = firstNo + secondNo;

为...

Int16 answer = (Int16)(firstNo + secondNo);

Microsoft converts your Int16 variables into Int32 when doing the add function.

Change the following:

Int16 answer = firstNo + secondNo;

into...

Int16 answer = (Int16)(firstNo + secondNo);
烛影斜 2024-11-12 07:30:15

将两个 Int16 值相加会产生一个 Int32 值。您必须将其转换为 Int16

Int16 answer = (Int16) (firstNo + secondNo);

您可以通过将所有数字切换为 Int32 来避免此问题。

Adding two Int16 values result in an Int32 value. You will have to cast it to Int16:

Int16 answer = (Int16) (firstNo + secondNo);

You can avoid this problem by switching all your numbers to Int32.

心房敞 2024-11-12 07:30:15

两个 Int16 变量相加的结果是一个 Int32

Int16 i1 = 1;
Int16 i2 = 2;
var result = i1 + i2;
Console.WriteLine(result.GetType().Name);

它输出 Int32

The result of summing two Int16 variables is an Int32:

Int16 i1 = 1;
Int16 i2 = 2;
var result = i1 + i2;
Console.WriteLine(result.GetType().Name);

It outputs Int32.

征棹 2024-11-12 07:30:15

问题是,正如其他人已经指出的那样,添加两个 Int16 会产生 Int32
您的第二个问题,为什么在声明这两个变量时尚未出现此问题,解释如下:http://msdn.microsoft.com/en-us/library/ybs77ex4%28v=VS.71%29.aspx

<前><代码>短x = 32767;

在前面的声明中,整数文字 32767 从 int 隐式转换为 Short。如果整数文字不适合短存储位置,则会发生编译错误。

因此,它在您的声明中起作用的原因很简单,即已知所提供的文字适合 short

The problem is, that adding two Int16 results in an Int32 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:

short x = 32767;

In the preceding declaration, the integer literal 32767 is implicitly converted from int to short. If the integer literal does not fit into a short storage location, a compilation error will occur.

So, the reason why it works in your declaration is simply that the literals provided are known to fit into a short.

娇纵 2024-11-12 07:30:15

加号运算符首先将操作数转换为 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.

多情出卖 2024-11-12 07:30:15

由于某些奇怪的原因,您可以使用 += 运算符来添加短裤。

short answer = 0;
short firstNo = 1;
short secondNo = 2;

answer += firstNo;
answer += secondNo;

For some strange reason, you can use the += operator to add shorts.

short answer = 0;
short firstNo = 1;
short secondNo = 2;

answer += firstNo;
answer += secondNo;
尾戒 2024-11-12 07:30:15

该行

 Int16 answer = firstNo + secondNo;

被解释为

 Int16 answer = (Int32) (firstNo + secondNo);

Simply,因为不存在 Int16 算术之类的东西。

简单的解决方案:不要使用 Int16。使用 Int32 或简单地使用 int

int 是默认的整数类型。短和长仅在特殊情况下使用。

The line

 Int16 answer = firstNo + secondNo;

is interpreted as

 Int16 answer = (Int32) (firstNo + secondNo);

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.

泪痕残 2024-11-12 07:30:15

这是因为两个 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 an Int32.
Check the "conversions" paragraph here: http://msdn.microsoft.com/en-us/library/ybs77ex4%28v=vs.71%29.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文