是铸造同样的事情转换? [英] Is casting the same thing as converting?

查看:100
本文介绍了是铸造同样的事情转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在杰西自由的学习C#的书,他说,一类的对象可以被转换成另一种类型的对象。这就是所谓的铸造。

In Jesse Liberty's Learning C# book, he says "Objects of one type can be converted into objects of another type. This is called casting."

如果您调查从下面的code产生IL,你可以清楚地看到,铸造分配没有做同样的事情作为转换任务。在前者,你可以看到发生的装箱/拆箱;在后者,你可以看到一个转换方法的调用。

If you investigate the IL generated from the code below, you can clearly see that the casted assignment isn't doing the same thing as the converted assignment. In the former, you can see the boxing/unboxing occurring; in the latter you can see a call to a convert method.

我知道它到底可能只是一个愚蠢的语义差别 - 但铸造只是一个字的转换。我不是说是表露无疑,但我不感兴趣,在这个人的直觉 - 观点并不指望在这里!任何人都可以指向证实或否认,如果铸造和转换是一回事吗?

I know in the end it may be just a silly semantic difference--but is casting just another word for converting. I don't mean to be snarky, but I'm not interested in anyone's gut feeling on this--opinions don't count here! Can anyone point to a definitive reference that confirms or denies if casting and converting are the same thing?

    object x;
    int y;

    x = 4;

    y = ( int )x;

    y = Convert.ToInt32( x );

感谢您

RP

请注意马特的有关意见后增加显/隐:

Note added after Matt's comment about explicit/implicit:

我不认为隐性/明确的区别。在code我张贴,变化是在两种情况下明确。隐式转换是当你分配一个短为int发生的情况。

I don't think implicit/explicit is the difference. In the code I posted, the change is explicit in both cases. An implicit conversion is what occurs when you assign a short to an int.

请注意,以Sklivvz:

Note to Sklivvz:

我想确认我的杰西自由的(否则通常是清晰和明确的)语言松动的怀疑是正确的。我以为杰西自由当时正在用他的语言有点松。据我所知,铸造对象层次路由 - 即,你不能从一个整数转换为一个字符串,但是你可以从从System.Exception派生于System.Exception的自定义异常施放。

I wanted confirmation that my suspicion of the looseness of Jesse Liberty's (otherwise usually lucid and clear) language was correct. I thought that Jesse Liberty was being a little loose with his language. I understand that casting is routed in object hierarchy--i.e., you can't cast from an integer to a string but you could cast from custom exception derived from System.Exception to a System.Exception.

有意思的是,虽然,当你尝试从一个int转换为字符串,编译器会告诉你,它不能转换的价值。也许杰西更正确的比我想象的!

It's interesting, though, that when you do try to cast from an int to a string the compiler tells you that it couldn't "convert" the value. Maybe Jesse is more correct than I thought!

推荐答案

简单的答案是:这取决于

The simple answer is: it depends.

对于值类型,铸件将涉及到它真正转换为不同的类型。例如:

For value types, casting will involve genuinely converting it to a different type. For instance:

float f = 1.5f;
int i = (int) f; // Conversion

在浇注前pression unboxes,结果(假设它的作品)是的一般的只是一个什么在框中,与同类型的副本。但也有例外,但是 - 你可以从一个装箱的int拆箱到一个枚举(具有基础类型为int),反之亦然;同样,你可以从一个装箱的int为可空&LT拆箱; INT方式>

When the casting expression unboxes, the result (assuming it works) is usually just a copy of what was in the box, with the same type. There are exceptions, however - you can unbox from a boxed int to an enum (with an underlying type of int) and vice versa; likewise you can unbox from a boxed int to a Nullable<int>.

在浇注前pression从一个引用类型到另一个没有用户定义的转换涉及,也没有转换就本身而言对象 - 只有的参考改变 - 这就是真的只有该值被认为的方式,而不是引用本身(会前相同位)。例如:

When the casting expression is from one reference type to another and no user-defined conversion is involved, there's no conversion as far as the object itself is concerned - only the type of the reference "changes" - and that's really only the way that the value is regarded, rather than the reference itself (which will be the same bits as before). For example:

object o = "hello";
string x = (string) o; // No data is "converted"; x and o refer to the same object

在用户定义的转换动手,这的一般的返回嗣继承不同的对象/值。例如,您可以定义一个转换为字符串您自己的类型 - 和
 这肯定不会是相同的数据作为自己的目标。 (这可能是一个现有的字符串提到了你的对象已经,当然)。以我的经验用户定义的转换通常是值类型,而不是引用类型之间存在的,所以这是很少的问题。

When user-defined conversions get involved, this usually entails returning a different object/value. For example, you could define a conversion to string for your own type - and this would certainly not be the same data as your own object. (It might be an existing string referred to from your object already, of course.) In my experience user-defined conversions usually exist between value types rather than reference types, so this is rarely an issue.

所有这些计数为转换在规范方面的 - 但他们并不都算作一个转换的对象的成不同类型的的对象的。我怀疑这是杰西自由是与术语松动的情况 - 我注意到,在编程​​C#3.0,这是我刚刚在读

All of these count as conversions in terms of the specification - but they don't all count as converting an object into an object of a different type. I suspect this is a case of Jesse Liberty being loose with terminology - I've noticed that in Programming C# 3.0, which I've just been reading.

这是否面面俱到?

这篇关于是铸造同样的事情转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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