转换和转换是一样的吗? [英] Is casting the same thing as converting?

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

问题描述

在 Jesse Liberty 的 Learning 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."

如果您调查从下面的代码生成的 IL,您可以清楚地看到强制转换的分配与转换后的分配做的事情不同.在前者中,您可以看到装箱/拆箱正在发生;在后者中,您可以看到对 convert 方法的调用.

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

在 Matt 关于显式/隐式的评论后添加的注释:

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

我不认为隐式/显式是区别.在我发布的代码中,两种情况下的更改都是明确的.将 short 分配给 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:

我想确认我对 Jesse Liberty(否则通常清晰明了)语言松散的怀疑是正确的.我认为 Jesse Liberty 的语言有点松散.我知道转换是在对象层次结构中路由的 - 即,您不能从整数转换为字符串,但您可以从从 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

当转换表达式取消装箱时,结果(假设它有效)通常只是框中内容的副本,具有相同的类型.但是,也有例外 - 您可以从装箱的 int 拆箱到 enum(具有 int 基础类型),反之亦然;同样,您可以将装箱的 int 拆箱为 Nullable.

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

当转换表达式是从一种引用类型到另一种引用类型并且不涉及用户定义的转换时,就对象本身而言没有转换——只有引用的类型发生了变化" - 这实际上只是考虑值的方式,而不是引用本身(这将是与以前相同的位).例如:

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.

根据规范,所有这些都算作转换 - 但它们并不都算作将 object 转换为不同类型的 object.我怀疑这是 Jesse Liberty 对术语松散的一个案例 - 我在编程 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天全站免登陆