C#4.0中的隐式转换规则 [英] implicite conversion rule in c# 4.0

查看:65
本文介绍了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,

这是一个棘手的问题.仍然没有定义从ushortchar的隐式数值转换.反之亦然,从charushort.
简而言之,没有隐式转换为char类型,因此其他整数类型的值不会自动转换为char类型.

隐式数值转换为:
  • sbyte到short,int,long,float,double或十进制.
Hi rtn,

This is a tricky question. Still, there is no implicit numeric conversion defined for ushort to char. The only valid thing is vice-versa, from char to ushort.
In short, there are no implicit conversions to char type, so the values of the other integral types do not automatically convert to the char 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屋!

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