C#-转换是否保留对原始对象的引用? [英] C# - Does casting preserve the reference to the original object?

查看:58
本文介绍了C#-转换是否保留对原始对象的引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用C#转换对象,它是否仍引用原始对象转换?

If I cast an object in C#, does it still reference the original object cast?

例如,如果我创建了一个方法来更改对象,然后根据所调用的特定实现强制转换该对象,那么是否保留了原始对象?

As an example, if I create a method to change an object and then cast that object depending on specific implementation called, does the original object get preserved?

    public bool CallChangeString()
    {
        String str = "hello";
        ChangeObject(str);
        return String.Equals("HELLO", str);
    }

    public void ChangeObject(Object obj)
    {
        String str = obj as String;
        str.ToUpper(); // pretend for the sake of this that this changes str to upper case
    }

在这种情况下,String.Equals是否返回true或false?

In this case, would the String.Equals return true or false?

在任何情况下,强制转换都会导致新对象无法保留其引用吗?

Are there any situations where casting would cause the new object to not preserve it's reference?

推荐答案

取决于您正在执行的转换.您可以想到两种主要的铸造类型:

Depends on what cast you are doing. You can think of two main groups of casting:

  1. 保留身份的人
  2. 那些不是

嗯,那不是很有帮助,这正是您要问的.

Well, thats not very helpful, that is precisely what you are asking.

什么演员保留身份?所有那些需要在对象本身中进行表示性更改的对象,即构成对象 change bits .

What casts don't preserve identity? All those that entail a representational change in the object itself, that is, the bits that make up the object change.

例子?所有值类型之间的转换: int long int double 等,任何装箱或拆箱转换等等.组成 long 的位与组成 int 的位非常不同.

Examples? All casts between value types: int to long, int to double, etc., any boxing or unboxing conversion, etc. The bits that make up a long are very different from those that make up an int.

更多示例?任何用户定义的强制转换运算符,无论是显式的还是隐式的.为什么?因为编译器不允许用户保留用于保留身份的强制类型转换,只是因为编译器已经为您完成了这些操作:

More examples? Any user defined cast operator, explicit or implicit. Why? Because user defined casts that preserve identity are disallowed by the compiler simply because the compiler already does them for you:

class Foo : IFoo
{
     public static implicit operator object(Foo foo) => return foo; //Compile time error     
     public static implicit operator IFoo(Foo foo) => return foo; //compile time error.

     public static explicit operator Bar(Foo foo) => return new Bar(foo);
}

请注意用户定义的强制转换的一般模式:

Note the general pattern of user defined casts:

  • 任何有效的运算符将始终具有 new 潜伏无论它返回什么.那就告诉你,强制转换无法保留身份...您要返回一个新对象.
  • 您无法实现用户定义的身份保留转换.没问题,因为不需要 ,这些转换已经由C#的类型系统提供.
  • any valid operator will always have a new lurking around in whatever it returns. Thats telling you right away that the cast can not preserve identity... you are returning a new object.
  • There is no way you can implement a user defined identity preserving conversion. Thats not a problem because there is no need, those conversions are already provided by C#'s type system.

那么,什么 身份保留类型或转换?好吧,那些触摸所有物体的人;参考转化.

So, what are identity preserving casts or conversions? Well, those that don't touch the object all; reference conversions.

嗯?但是,等等,要点是我正在投射对象,那怎么可能呢?

Huh? But wait, The whole point is that I'm casting the object, how can that be?

在引用转换中,您仅投射"引用指向该对象.对象始终是相同的.当然,这仅在引用类型中才有意义,这就是为什么值类型不具有身份保留转换的原因.除非将它们装箱,否则不会引用值类型.

In reference conversions you are only "casting" the reference pointing to the object. The object is always the same. This, of course, only makes sense in reference types and that is why value types don't have identity preserving conversions; there are no references to value types unless they are boxed.

例子? object string Foo IFoo Giraffe Animal 等.

请注意,引用类型也可以很好地实现用户定义的强制类型转换,但是它们不会保留身份.

Do note that reference types can very well implement user defined casts too, but these will not preserve identity.

所有这些,这是经验法则:

All that said, this is the rule of the thumb:

  • 语言类型系统本身提供/允许的任何类型转换都会保留身份.这些仅是引用转换,并且仅适用于引用类型.
  • 任何用户定义的类型转换,无论是显式的还是隐式的,都不会保留身份(记住,(long)2 是用户定义的类型转换,有人必须在类 System.Int64 ).
  • Any cast provided/allowed by the language type system itself preserves identity. These are, exclusively, reference conversions and only apply to reference types.
  • Any user defined cast, explicit or implicit, does not preserve identity (rememeber, (long)2 is a user defined cast, somebody had to implement it in the class System.Int64).

因此,回答您的问题, var str = obj作为字符串是参考转换,这意味着 ReferenceEquals(str,obj) true ; str obj 指向完全相同的对象,唯一的区别是引用的类型.

So, answering your question, var str = obj as string is a reference conversion, which means that ReferenceEquals(str, obj) is true; str and obj point to exactly the same object, the only difference is the type of the reference.

这篇关于C#-转换是否保留对原始对象的引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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