By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,011 Members | 2,967 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,011 IT Pros & Developers. It's quick & easy.

know bug, bug, or me being stupid. Immediate cast bug..

P: n/a
PT
Hi, not sure if this is just me, an already known bug or what, but if anyone
can shed light on why this does what it does it would be greatly
appreciated, it seems to work as expected if you're dealing with Int32
variables rather than immediates.

public class Test
{
public void Tester( object o )
{
Console.WriteLine("not a byte");
}
public void Tester(byte o)
{
Console.WriteLine("is a byte");
}
};

Test t = new Test();
t.Tester((byte)0);
t.Tester((UInt32)0);
t.Tester((Int32)0); <--- reports "is a byte"

Regards,

Paul
Aug 1 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
PT wrote:
Hi, not sure if this is just me, an already known bug or what, but if anyone
can shed light on why this does what it does it would be greatly
appreciated, it seems to work as expected if you're dealing with Int32
variables rather than immediates.
It's not actually a bug, although I agree it looks that way at
first.(Int32)0 is still a constant expression (of type int), and so is
implicitly convertible to byte according to the spec.

Jon

Aug 1 '06 #2

P: n/a
PT
Yeah, I guess that makes sense, albeit confusing if you see said code being
passed to a stream writer :) Thanks again.

Paul.

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11*********************@75g2000cwc.googlegrou ps.com...
PT wrote:
>Hi, not sure if this is just me, an already known bug or what, but if
anyone
can shed light on why this does what it does it would be greatly
appreciated, it seems to work as expected if you're dealing with Int32
variables rather than immediates.

It's not actually a bug, although I agree it looks that way at
first.(Int32)0 is still a constant expression (of type int), and so is
implicitly convertible to byte according to the spec.

Jon

Aug 1 '06 #3

P: n/a
Jon Skeet [C# MVP] wrote:
PT wrote:
Hi, not sure if this is just me, an already known bug or what, but if anyone
can shed light on why this does what it does it would be greatly
appreciated, it seems to work as expected if you're dealing with Int32
variables rather than immediates.

It's not actually a bug, although I agree it looks that way at
first.(Int32)0 is still a constant expression (of type int), and so is
implicitly convertible to byte according to the spec.
<spec section 13.1.7>

An implicit constant expression conversion permits the following
conversions:
A constant-expression (14.16) of type int can be converted to
type sbyte, byte, short, ushort, uint, or ulong, provided the value of
the constant-expression is within the range of the destination type.

</spec>

Never would have guessed. You learn something new every day.

Michael

Aug 1 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.