Re: How to cast int to short
- From: "Derrick Coetzee [MSFT]" <dcoetzee@xxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 16 Feb 2006 11:14:36 -0800
Jon Skeet [C# MVP] wrote:
Derrick Coetzee [MSFT] <dcoetzee@xxxxxxxxxxxxxxxxxxxx> wrote:
myFun ((short)i);
Note that, by default, C# checks for integer overflow when
performing a cast in this obvious way. [...]
No, by default C# *doesn't* check for overflow, except for
compile-time constant expressions.
Sorry, my mistake - I'm used to this option being on. I do personally
recommend that the /checked option (Build->Advanced->Check for arithmetic
overflow/underflow) is always used, as this can help to find bugs and
prevent potential security issues, as well as provide conceptual safety
guarantees (and I think it should be on by default). If profiling reveals
that they're slowing down a bottleneck, or if you want the C behaviour of
arithmetic mod 2^word size, you can always wrap the relevant code in
unchecked { }. Thanks for the correction, Jon.
--
Derrick Coetzee, MCAD, MSFT (Speech Server)
This posting is provided "AS IS" with no warranties, and confers no
rights.
.
- References:
- How to cast int to short
- From: ad
- Re: How to cast int to short
- From: Jon Skeet [C# MVP]
- Re: How to cast int to short
- From: Derrick Coetzee [MSFT]
- Re: How to cast int to short
- From: Jon Skeet [C# MVP]
- How to cast int to short
- Prev by Date: Re: convert of VB fix function
- Next by Date: Data binding on usercontrol does not work
- Previous by thread: Re: How to cast int to short
- Next by thread: Search through list of tasks in task manager
- Index(es):
Relevant Pages
|