理论问题 [英] theoretic question

查看:89
本文介绍了理论问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

为什么我们需要在这里明确演员?


短i = 0;

// i = i + 1; //无法将类型''int''隐式转换为''short''

// i = i +(short)1; //无法将类型''int''隐式转换为''short''

i =(短)(i + 1);


谢谢,

fleimeris

解决方案

2004年1月27日星期二18:16:39 +0200,fleimeris写道:

你好,
为什么我们需要在这里进行显式演员?
short i = 0;
// i = i + 1; //不能隐式地将类型''int''转换为''short''
// i = i +(short)1; //不能隐式地将类型''int''转换为''short''
i =(short)(i + 1);
谢谢,
fleimeris




这个页面给出了一个简短的解释;

http://msdn.microsoft.com/library/en...vclrfShort。 asp


HTH,

Tim

-

给我发电子邮件,让鼻涕变热。


> short i = 0;

// i = i + 1; //不能隐式地将类型''int''转换为''short''
// i = i +(short)1; //无法将类型''int''隐式转换为''short''
i =(short)(i + 1);



1个文字是一个整数,所以右侧的表达式评估

为整数。在将它存储在

变量i之前,它会被转换回短路。转换可能会抛出溢出异常。它需要显式强制转换才能让你知道可能的

异常。


问候,

Wessel


Fleimeris,


整数常量计算为int。一个int *可以*溢出一个短的。

明确说明你的意图更安全,这不仅使得b / b
代码更加不言而喻,而且确保开发人员意识到他们正在做什么?b $ b。 C#在溢出安全时有一些隐含的转换,

但是否则倾向于支持explyiness而不是太聪明一半。


当然编译器可以知道一个常量,其计算结果小于或等于System.Int16.MaxValue不会导致溢出,但是我认为他们决定了一致性和性能比试图检查这个更重要。


我离开我的开发机器并且不记得肯定但我认为

你能做到:


长lngFoo = 0L;


如果是这样,我想知道是否有相同的表示法对于短裤,例如,

" 0S"?您可以在帮助中查看。这样可以节省运行时

cast。或者你可以:


const short shOne =(短)1;

短i = 0;

i = i + shOne ;


当然:


短i = 0;

i ++;

....如果你只是想增加它,那将是最好的,但我认为你只需要设计一个例子。


作为最后一点,在
运行时使用byte或short通常没有优势,而不是int;实际上,int可以更快。你只使用short,如果你正在构建一个大型数组或值集合,并希望以一定的代价节省内存以获得访问速度。我的理解是CLR是针对int操作进行优化的b $ b,至少在32位硬件上是这样的。


--BOB

fleimeris <我*** @ mail.lt>在消息中写道

新闻:%2 **************** @ TK2MSFTNGP11.phx.gbl ...

你好,
为什么我们需要在这里进行显式演员?

短片i = 0;
// i = i + 1; //不能隐式地将类型''int''转换为''short''
// i = i +(short)1; //无法将类型''int''隐式转换为''short''
i =(短)(i + 1);

谢谢,
fleimeris


hello,
why do we need explicit cast here ?

short i = 0;
//i = i + 1; // Cannot implicitly convert type ''int'' to ''short''
//i = i + (short)1; // Cannot implicitly convert type ''int'' to ''short''
i = (short)(i + 1);

thanks,
fleimeris

解决方案

On Tue, 27 Jan 2004 18:16:39 +0200, fleimeris wrote:

hello,
why do we need explicit cast here ? short i = 0;
//i = i + 1; // Cannot implicitly convert type ''int'' to ''short''
//i = i + (short)1; // Cannot implicitly convert type ''int'' to ''short''
i = (short)(i + 1); thanks,
fleimeris



This page gives a brief explanation;

http://msdn.microsoft.com/library/en...vclrfShort.asp

HTH,
Tim
--
To email me, make the snot hot.


> short i = 0;

//i = i + 1; // Cannot implicitly convert type ''int'' to ''short''
//i = i + (short)1; // Cannot implicitly convert type ''int'' to ''short''
i = (short)(i + 1);


The 1 literal is an integer, so the expression on the right side evaluates
to integer. This is converted back to short before it is stored in
variable i. The conversion may throw an overflow exception. It makes
sense to require explicit casting to make you aware of the possible
exception.

Greetings,
Wessel


Fleimeris,

An integer constant evaluates to an int. An int *could* overflow a short.
It is safer to explicitly state your intentions which not only makes the
code more self-evident but insures that the developer is aware of what they
are doing. C# has some implicit conversions when they are overflow-safe,
but otherwise tends to favor explitiness over being "too clever by half".

Of course the compiler could know that a constant that evaluates to less
than or equal to System.Int16.MaxValue would not result in an overflow but I
suppose they decided consistency and performance was more important than
trying to check for this.

I am away from my development machine and don''t recall for sure but I think
you can do:

long lngFoo = 0L;

If so, I wonder whether there is an equivalent notation for shorts, e.g.,
"0S"? You might check it out in the help. That would save the runtime
cast. Or you could:

const short shOne = (short)1;
short i = 0;
i = i + shOne;

Of course:

short i = 0;
i++;

.... would be best of all if you just want to increment it, but I assume you
are just contriving an example.

As a final note, there is generally no advantage to using byte or short at
runtime rather than int; indeed, int can be faster. You only use short if
you are building a large array or collection of values and want to conserve
memory at some expense to access speed. My understanding is that the CLR is
optimized for int operations, at least on 32-bit hardware.

--Bob
"fleimeris" <me***@mail.lt> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...

hello,
why do we need explicit cast here ?

short i = 0;
//i = i + 1; // Cannot implicitly convert type ''int'' to ''short''
//i = i + (short)1; // Cannot implicitly convert type ''int'' to ''short''
i = (short)(i + 1);

thanks,
fleimeris



这篇关于理论问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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