(我||编译器)==愚蠢 [英] (Me || Compiler) == Stupid

查看:112
本文介绍了(我||编译器)==愚蠢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void Test(短i,短j)

{

短k = i + j;

}


这段代码到底怎么了?

不能隐式地将int类型转换为short是编译错误。

ASP.NET 1.1 C#


谢谢

private void Test(short i, short j)
{
short k = i + j;
}

What the hell is wrong with this code?
"Can not implicitly convert type int to short" is the compile error.
ASP.NET 1.1 C#

Thanks

推荐答案

它不是非常直观,被授予,但你只需要施放:


短k =(短)(i + j);


在这里讨论更多(但并没有说明为什么):
http://msdn2.microsoft.com/en-us/library/ybs77ex4.aspx


Marc
It isn''t very intuitive, granted, but you just need to cast:

short k = (short)(i + j);

Discussed more here (but doesn''t really state "why"):
http://msdn2.microsoft.com/en-us/library/ybs77ex4.aspx

Marc


''短+短''可以大于''短'',你不能含蓄地

转换它因尺寸而变短。你必须明确地做到这一点:


private void测试(短i,短j)

{

短k =(简短)(i + j);

}


" Steven Nagy"写道:
''Short + short'' can be bigger than a ''short'', and you can not implicitly
convert it to a short value due to the size. You have to do it explicitly:

private void Test(short i, short j)
{
short k = (short) (i + j);
}

"Steven Nagy" wrote:
private void Test(short i,short j)
{
短k = i + j;
}
<这个代码到底怎么了?
不能隐式地将int类型转换为short是编译错误。
ASP.NET 1.1 C#

谢谢
private void Test(short i, short j)
{
short k = i + j;
}

What the hell is wrong with this code?
"Can not implicitly convert type int to short" is the compile error.
ASP.NET 1.1 C#

Thanks



对我来说这是框架中的不一致;


Int32 + Int32可以比Int32(int)更长,但它默默地这样做

(包装),返回一个Int32。

与Int64(long)同样,返回Int64;但是Int16(短)在总和上返回

Int32。


*据推测*这是出于一些有意义的原因...不太确定是什么/>
但是;任何人?


Marc
To me this is an inconsistency in the framework;

Int32 + Int32 can be longer than an Int32 (int), but it does this silently
(wrapping), returning an Int32.
Likewise with Int64 (long), returning an Int64; yet Int16 (short) returns an
Int32 on summation.

*Presumably* this was for some some meaningful reason... not quite sure what
it is though; anybody?

Marc


这篇关于(我||编译器)==愚蠢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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