difference between type conversion and type casting? [duplicate]

☆樱花仙子☆ 提交于 2019-12-19 04:59:36

问题


Possible Duplicate:
What is the difference between casting and conversion?

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


回答1:


First off, this is a duplicate of

What is the difference between casting and conversion?

which was inspired by

What is the (type) in (type)objectname.var

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

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

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.

What do we learn from this?

  • 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 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.

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

And so on.

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/tags/conversions/




回答2:


Conversion = actually transforming an object into an instance of a different class ex:

int i = 3; 
string str = i.ToString();

Transforms an integer into a string

Casting = forcing the type of an object because you know more than the compiler ex:

object obj = 3;
int i = (int)obj;



回答3:


Here's a good thread with some examples and answers to your question.



来源:https://stackoverflow.com/questions/3770625/difference-between-type-conversion-and-type-casting

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!