[求助]显式转换问题
各位大侠们,我是一个新手,前几天做了一个代码:
using System;
using System.Windows.Forms;
public class test
{
static void Main()
{
float a=2.8f;
long x=4249967295;
long b=2294677295;
int c=200000000;
int y=100;
int z=100;
int y=(int )x;
int z=z(int ) (b+c);
MessageBox.show(string.format("x={0} y={1}\nz={2} b={3} c={4}",x, y, b, c, z));
}
}
这样不行,提示说y 和 z 已经在局部变量中声明,不能进行显式转换,而把代码改成这样却又行了:using System.Windows.Forms;
public class test
{
static void Main()
{
float a=2.8f;
long x=4249967295;
long b=2294677295;
int c=200000000;
int y=100;
int z=100;
int y=(int )x;
int z=z(int ) (b+c);
MessageBox.show(string.format("x={0} y={1}\nz={2} b={3} c={4}",x, y, b, c, z));
}
}
using System;
using System.Window.Forms;
public class test
{
static void Main()
{
float a=2.8f;
long x=4249967295;
long b=2294677295;
int c=200000000;
int y=100;
int z=100;
try
{
checekd
{
y=(int )x;
z=z(int ) (b+c);
}
}
catch (Exception m)
{
MessageBox.show(m.Message);
}
MessageBox.show(string.format("x={0} y={1}\nz={2} b={3} c={4}",x, y, b, c, z));
}
}
改成这样就行了,问一下显式转换时(泛蓝)前面写不写所转换的数据类型有什么区别,比如上面的:int y=(int )x;using System.Window.Forms;
public class test
{
static void Main()
{
float a=2.8f;
long x=4249967295;
long b=2294677295;
int c=200000000;
int y=100;
int z=100;
try
{
checekd
{
y=(int )x;
z=z(int ) (b+c);
}
}
catch (Exception m)
{
MessageBox.show(m.Message);
}
MessageBox.show(string.format("x={0} y={1}\nz={2} b={3} c={4}",x, y, b, c, z));
}
}
int z=z(int ) (b+c); 和 y=(int )x;
z=z(int ) (b+c);