类型转换和类型转换之间的区别是什么? [英] difference between type conversion and type casting?

查看:188
本文介绍了类型转换和类型转换之间的区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  <一href="http://stackoverflow.com/questions/3166840/what-is-the-difference-between-casting-and-conversion">What是的铸造和转换之间的区别是什么?

类型转换和类型转换之间的区别是什么? 二会更好,如果你用一个例子做吧..

difference between type conversion and type casting? ii will be better if you make it by an example..

推荐答案

首先,这是一个重复

<一个href="http://stackoverflow.com/questions/3166840/what-is-the-difference-between-casting-and-conversion">http://stackoverflow.com/questions/3166840/what-is-the-difference-between-casting-and-conversion

这是灵感来自

<一个href="http://stackoverflow.com/questions/3166349/what-is-the-type-in-typeobjectname-var">http://stackoverflow.com/questions/3166349/what-is-the-type-in-typeobjectname-var

我会读这些,然后再回到这个简短的总结。

I would read those first, and then come back to this brief summary.

请您看看说明书第6章的第一款,其中规定:

I refer you to the first paragraph of chapter 6 of the specification, which states:

一个转换使得一个前pression到   被视为是一个特定   类型。 A转换可能会导致   给定类型的前pression是   视为具有不同的类型,或者   这可能会导致前pression无   键入得到一个类型。转换可   隐式或显式的,而这   确定一个明确的转换是否是   需要。例如,转换   从类型为int长的类型   int类型的隐式的,所以EX pressions   可以隐式地被视为类型   长。相反的转换,从   long类型为int类型,是明确的,   所以显式的转换是必需的。

A conversion enables an expression to be treated as being of a particular type. A conversion may cause an expression of a given type to be treated as having a different type, or it may cause an expression without a type to get a type. Conversions can be implicit or explicit, and this determines whether an explicit cast is required. For instance, the conversion from type int to type long is implicit, so expressions of type int can implicitly be treated as type long. The opposite conversion, from type long to type int, is explicit and so an explicit cast is required.

什么是我们学习呢?

  • A 转换的是语义操作的两个操作数: EX pression 的和的键入

  • A conversion is a semantic operation on two operands: expression and a type.

由语义分析确定确切的操作确定的实际值如何转换在运行时

The exact operation determined by the semantic analysis determines how the actual value is converted at runtime.

A 铸造的是语法元素的C#语言形式的(型)EX pression ,其中明确诱导的转换

A cast is a syntactic element of the C# language of the form (type)expression which explicitly induces a conversion from the expression to the type.

这些只是一小部分的的转换,你可以在C#做的:

These are just a few of the implicit conversions you can do in C#:

short aa = 123;      // numeric constant conversion from int to short
int bb = aa;         // numeric conversion from short to int
int? cc = null;      // nullable conversion from null literal to nullable int.
object dd = "hello"; // implicit reference conversion from string to object
IEnumerable<Giraffe> ee = new Giraffe[] { new Giraffe() } ;
                     // implicit reference conversion from array to sequence
IEnumerable<Animal> ff = ee; 
                     // implicit reference conversion involving array covariance
ff = null;           // implicit reference conversion from null literal to sequence
bb = 456;            // implicit identity conversion from int to int
dd = bb;             // implicit boxing conversion from int to object
Func<int, int> gg = x=>x+x; 
                     // anonymous function to delegate type conversion 

明确的转换通常,但并非总是如此,需要投:

Explicit conversions usually, but not always, require a cast:

aa = (short)bb;      // explicit numeric conversion
string hh = (string)dd; //explicit reference conversion

等等。

这是法律的使用塑像为隐式转换,但通常没有必要。

It is legal to use a cast for an implicit conversion but usually not necessary.

清楚了吗?关键的一点是,的转换是语义操作的而导致的在运行时的动作铸造是一个语法元素,它告诉编译器来分析转换使用显式转换分析规则的。

Is that clear? The key point is that a conversion is a semantic operation which leads to an action at runtime and a cast is a syntactic element that tells the compiler to analyze a conversion using the explicit conversion analysis rules.

如果转换逻辑的主题感兴趣,那么你可能会感兴趣我的文章的主题,这是在这里:

If the subject of conversion logic interests you then you might be interested in my articles on the subject, which are here:

http://blogs.msdn.com/b/ericlippert/archive /标签/转换/

这篇关于类型转换和类型转换之间的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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