C#4.0中的隐式转换规则 [英] implicite conversion rule in c# 4.0
问题描述
将一种类型的数据分配给另一种类型时,如果
,则将自动进行隐式转换
1>两种类型兼容
2>目标类型的范围大于源类型.
(来自c#4.0的完整参考.作者:Herbert Schildt)
现在遵循此规则
char ch;
ushort vushort;
ch = vshort; //错误:隐式转换不是从ushort到char
现在我的问题是:
char是一个无符号的16位数据.而ushort也是一个无符号的16位深度.
那为什么不能进行隐式转换.
根据上述规则给出答案..
When one type of data is assigned to another type an implicit conversion will take place automatically if
1>The two types are compatible
2>Destination type has a range that is greater than sourse type.
(reference from c# 4.0 complete reference.by Herbert Schildt)
Now following this rule
char ch;
ushort vushort;
ch=vshort; //error:implicit conversion not from ushort to char
Now my question is that:
char is an unsigned 16 bit data.And ushort also unsigned 16 bit depth.
then why implicite conversion not possible.
Give answer in accordance to rule stated above..
推荐答案
您好rtn,
这是一个棘手的问题.仍然没有定义从ushort
到char
的隐式数值转换.反之亦然,从char
到ushort
.
简而言之,没有隐式转换为char
类型,因此其他整数类型的值不会自动转换为char
类型.
隐式数值转换为:
- 从
sbyte
到short,int,long,float,double或十进制.
Hi rtn,
This is a tricky question. Still, there is no implicit numeric conversion defined forushort
tochar
. The only valid thing is vice-versa, fromchar
toushort
.
In short, there are no implicit conversions tochar
type, so the values of the other integral types do not automatically convert to thechar
type.
The implicit numeric conversions are:
- From
sbyte
to short, int, long, float, double, or decimal.
- 从
byte
到short,ushort,int,uint,long,ulong,float,double或十进制.
- From
byte
to short, ushort, int, uint, long, ulong, float, double, or decimal.
- 从
short
到int,long,float,double或十进制.
- From
short
to int, long, float, double, or decimal.
- 从
ushort
到int,uint,long,ulong,float,double或十进制.
- From
ushort
to int, uint, long, ulong, float, double, or decimal.
- 从
int
到long,float,double或小数.
- From
int
to long, float, double, or decimal.
- 从
uint
到long,ulong,float,double或十进制.
- From
uint
to long, ulong, float, double, or decimal.
- 从
long
到浮点,双精度或十进制.
- From
long
to float, double, or decimal.
- 从
ulong
到浮点,双精度或十进制.
- From
ulong
to float, double, or decimal.
- 从
char
到ushort,int,uint,long,ulong,float,double或十进制.
- From
char
to ushort, int, uint, long, ulong, float, double, or decimal.
- 从
float
翻倍.
char ch = '1';
ushort vushort;
vushort = ch;
希望这会有所帮助.
干杯
Hope this helps.
Cheers
这篇关于C#4.0中的隐式转换规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!