C#隐式转换
Java 和 C# 遵守相似的数据类型自动转换和强制转换规则。
同 Java 一样,C# 既支持隐式类型转换又支持显式类型转换。在扩大转换的情况下是C#隐式转换。例如,下面从 int 到 long 的转换是隐式的,如同 Java 中的一样:
代码如下 |
复制代码 |
int intVariable = 5; long l = intVariable; |
下面是 .NET 数据类型之间的C#隐式转换列表:
可以使用与 Java 一样的语法对希望显式转换的表达式进行强制转换:
代码如下 |
复制代码 |
long longVariable = 5483; int intVariable = (int)
|
C#中判断某类型是否可以进行隐式类型转换
代码如下 |
复制代码 |
static private bool CanConvert(Type from, Type to)
{
if (from.IsPrimitive && to.IsPrimitive)
{
TypeCode typeCodeFrom = Type.GetTypeCode(from);
TypeCode typeCodeTo = Type.GetTypeCode(to);
if (typeCodeFrom == typeCodeTo)
return true;
if (typeCodeFrom == TypeCode.Char)
switch (typeCodeTo)
{
case TypeCode.UInt16: return true;
case TypeCode.UInt32: return true;
case TypeCode.Int32: return true;
case TypeCode.UInt64: return true;
case TypeCode.Int64: return true;
case TypeCode.Single: return true;
case TypeCode.Double: return true;
default: return false;
}
// Possible conversions from Byte follow.
if (typeCodeFrom == TypeCode.Byte)
switch (typeCodeTo)
{
case TypeCode.Char: return true;
case TypeCode.UInt16: return true;
case TypeCode.Int16: return true;
case TypeCode.UInt32: return true;
case TypeCode.Int32: return true;
case TypeCode.UInt64: return true;
case TypeCode.Int64: return true;
case TypeCode.Single: return true;
case TypeCode.Double: return true;
default: return false;
}
// Possible conversions from SByte follow.
if (typeCodeFrom == TypeCode.SByte)
switch (typeCodeTo)
{
case TypeCode.Int16: return true;
case TypeCode.Int32: return true;
case TypeCode.Int64: return true;
case TypeCode.Single: return true;
case TypeCode.Double: return true;
default: return false;
}
// Possible conversions from UInt16 follow.
if (typeCodeFrom == TypeCode.UInt16)
switch (typeCodeTo)
{
case TypeCode.UInt32: return true;
case TypeCode.Int32: return true;
case TypeCode.UInt64: return true;
case TypeCode.Int64: return true;
case TypeCode.Single: return true;
case TypeCode.Double: return true;
default: return false;
}
// Possible conversions from Int16 follow.
if (typeCodeFrom == TypeCode.Int16)
switch (typeCodeTo)
{
case TypeCode.Int32: return true;
case TypeCode.Int64: return true;
case TypeCode.Single: return true;
case TypeCode.Double: return true;
default: return false;
}
// Possible conversions from UInt32 follow.
if (typeCodeFrom == TypeCode.UInt32)
switch (typeCodeTo)
{
case TypeCode.UInt64: return true;
case TypeCode.Int64: return true;
case TypeCode.Single: return true;
case TypeCode.Double: return true;
default: return false;
}
// Possible conversions from Int32 follow.
if (typeCodeFrom == TypeCode.Int32)
switch (typeCodeTo)
{
case TypeCode.Int64: return true;
case TypeCode.Single: return true;
case TypeCode.Double: return true;
default: return false;
}
// Possible conversions from UInt64 follow.
if (typeCodeFrom == TypeCode.UInt64)
switch (typeCodeTo)
{
case TypeCode.Single: return true;
case TypeCode.Double: return true;
default: return false;
}
// Possible conversions from Int64 follow.
if (typeCodeFrom == TypeCode.Int64)
switch (typeCodeTo)
{
case TypeCode.Single: return true;
case TypeCode.Double: return true;
default: return false;
}
// Possible conversions from Single follow.
if (typeCodeFrom == TypeCode.Single)
switch (typeCodeTo)
{
case TypeCode.Double: return true;
default: return false;
}
}
return false;
}
|