C#编译器Bug? [英] C# Compiler Bug?

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

问题描述

在这种情况下,我试图在一个字节值上执行数学函数,其中

函数返回的另一个字节值。似乎没有办法让b $ b说服编译器即使使用显式强制转换也能做到这一点。如果添加的

值是常量,则工作正常,但当一个函数无法正常工作时,它将无法正常工作。


这是编译错误还是我错过了什么?


// VS 2003.net C#代码

公共类Class1

{

public Class1()

{

}

私有字节ByteFunc()

{

返回(字节)1;

}

public void MyFunc()

{

byte byOut;

byOut = 1; //好的:没有错误

byOut = 1 * 1; //好的:没有错误

byOut =(byte)1 *(byte)2; //好的:没有错误

byOut = ByteFunc(); //好的:没有错误

byOut = ByteFunc()* 2; //错误:无法隐式转换''int''

到''byte''

byOut =(byte)ByteFunc()*(byte)2; //错误:不能隐含地

将''int''转换为''byte''

}

}


-

Darren

解决方案

试试这个:


byOut = Convert.ToByte(ByteFunc()* 2);

$ b $bJosé


" dlf" < dl*@discussions.microsoft.com>在消息中写道

news:5E ********************************** @ microsof t.com ...

在这种情况下,我试图在一个字节值上使用
函数返回的另一个字节值来表示数学函数。似乎没有办法说服编译器即使使用显式强制转换也能做到这一点。如果
添加的值是常量,则工作正常,但当一个函数无法正常工作时

这是编译器错误或我错过了什么?

// VS 2003.net C#代码
公共类Class1
{
public Class1()
{
}
私有字节ByteFunc()
{
返回(字节)1;
}
public void MyFunc()
{
byte byOut;
byOut = 1; //好的:没有错误
byOut = 1 * 1; //好的:没有错误
byOut =(byte)1 *(byte)2; //好的:没有错误

byOut = ByteFunc(); //好的:没有错误
byOut = ByteFunc()* 2; //错误:无法隐式转换''int''
到''byte''
byOut =(byte)ByteFunc()*(byte)2; //错误:不能隐含地将''int''转换为''byte''
}
}

-
Darren


dlf< dl *@discussions.microsoft.com>写道:

在这种情况下,我试图在一个字节值上使用
函数返回的另一个字节值来表示数学函数。似乎没有办法说服编译器即使使用显式强制转换也能做到这一点。如果添加的
值是常量,则工作正常,但当一个函数无法正常工作时。

这是编译器错误还是我遗漏了某些东西?


你错过了什么,我很害怕。

// VS 2003.net C#代码
公共类Class1
{
公共Class1()
{
}私有字节ByteFunc()
{
返回(字节)1;
}
public void MyFunc()
{
byte byOut;
byOut = 1; //好的:没有错误
byOut = 1 * 1; //好的:没有错误
byOut =(byte)1 *(byte)2; // OK:没有错误


编译器在编译时执行以上所有操作,并且可以看到

结果是在字节的边界内。 (参见ECMA的第13.1.6节

规范。)

byOut = ByteFunc(); //好的:没有错误


是的,正如你所料,毫无疑问。

byOut = ByteFunc()* 2; //错误:无法隐式转换''int''
到''byte''
byOut =(byte)ByteFunc()*(byte)2; //错误:无法隐式地将''int''转换为''byte''




在这两种情况下,表达式的类型(在右边

赋值运算符的一边)是int,因为实际上没有
运算符*(byte,byte) - 每个操作数都转换为int然后
使用
运算符*(int,int),其结果类型为int。 (参见

ECMA规范第14.7.1节。)


你需要做的就是投下整个表达式:

byOut =(byte)(ByteFunc()* 2);


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复小组,请不要给我发邮件


JoséJoye< jo ******* @ KILLTHESPAMSbluewin.ch>写道:

试试这个:

byOut = Convert.ToByte(ByteFunc()* 2);




没有必要调用方法来做到这一点 - 直接演员会做什么

罚款:


byOut =(byte)(ByteFunc() * 2);


重要的是要确保它是整个表达式

的演员表,而不仅仅是ByteFunc的结果( )(已经是一个

字节)。


-

Jon Skeet - < sk *** @ pobox .com>
http://www.pobox.com/~skeet

如果回复小组,请不要给我发邮件


In this case, I am trying to perfom a math function on a byte value with
another byte value returned by a function. There appears to be no way to
convince the compiler to do this even with explicit casts. Works fine if the
values being added are constants, but when one is a function it doesn''t work
correctly.

Is this a compiler bug or am I missing something?

// VS 2003.net C# code
public class Class1
{
public Class1()
{
}
private byte ByteFunc()
{
return (byte)1;
}
public void MyFunc()
{
byte byOut;
byOut = 1; // OK: No error
byOut = 1*1; // OK: No error
byOut = (byte)1*(byte)2; // OK: No error
byOut = ByteFunc(); // OK: No error
byOut = ByteFunc() * 2; // ERROR: Cannot implicitly convert ''int''
to ''byte''
byOut = (byte)ByteFunc() * (byte)2; // ERROR: Cannot implicitly
convert ''int'' to ''byte''
}
}

--
Darren

解决方案

Try this:

byOut = Convert.ToByte(ByteFunc() * 2);

José

"dlf" <dl*@discussions.microsoft.com> wrote in message
news:5E**********************************@microsof t.com...

In this case, I am trying to perfom a math function on a byte value with
another byte value returned by a function. There appears to be no way to
convince the compiler to do this even with explicit casts. Works fine if
the
values being added are constants, but when one is a function it doesn''t
work
correctly.

Is this a compiler bug or am I missing something?

// VS 2003.net C# code
public class Class1
{
public Class1()
{
}
private byte ByteFunc()
{
return (byte)1;
}
public void MyFunc()
{
byte byOut;
byOut = 1; // OK: No error
byOut = 1*1; // OK: No error
byOut = (byte)1*(byte)2; // OK: No error
byOut = ByteFunc(); // OK: No error
byOut = ByteFunc() * 2; // ERROR: Cannot implicitly convert ''int''
to ''byte''
byOut = (byte)ByteFunc() * (byte)2; // ERROR: Cannot implicitly
convert ''int'' to ''byte''
}
}

--
Darren



dlf <dl*@discussions.microsoft.com> wrote:

In this case, I am trying to perfom a math function on a byte value with
another byte value returned by a function. There appears to be no way to
convince the compiler to do this even with explicit casts. Works fine if the
values being added are constants, but when one is a function it doesn''t work
correctly.

Is this a compiler bug or am I missing something?
You''re missing something, I''m afraid.
// VS 2003.net C# code
public class Class1
{
public Class1()
{
}
private byte ByteFunc()
{
return (byte)1;
}
public void MyFunc()
{
byte byOut;
byOut = 1; // OK: No error
byOut = 1*1; // OK: No error
byOut = (byte)1*(byte)2; // OK: No error
The compiler does all of the above at compile time, and can see that
the result is in the bounds of byte. (See section 13.1.6 of the ECMA
spec.)
byOut = ByteFunc(); // OK: No error
Yup, as you''d expect, no doubt.
byOut = ByteFunc() * 2; // ERROR: Cannot implicitly convert ''int''
to ''byte''
byOut = (byte)ByteFunc() * (byte)2; // ERROR: Cannot implicitly
convert ''int'' to ''byte''



In both of these cases, the type of the expression (on the right hand
side of the assignment operator) is int, because there is actually no
operator *(byte, byte) - each operand gets converted to int and then
operator *(int, int) is used, which has a resulting type of int. (See
section 14.7.1 of the ECMA spec.)

What you need to do is cast the whole expression:

byOut = (byte) (ByteFunc()*2);

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


José Joye <jo*******@KILLTHESPAMSbluewin.ch> wrote:

Try this:

byOut = Convert.ToByte(ByteFunc() * 2);



There''s no need to call a method to do this - a straight cast will do
fine:

byOut = (byte) (ByteFunc()*2);

The important thing is to make sure that it''s the whole expression
which is cast, not just the result of ByteFunc() (which is already a
byte).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


这篇关于C#编译器Bug?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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