请问在C#中投对象总是引用返回到初始对象 [英] Does casting an Object in C# always return a Reference to the initial object

查看:110
本文介绍了请问在C#中投对象总是引用返回到初始对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在做一个项目在C#中使用Windows窗体工作。在它的过程中,我做了以下

I'm currently doing a project in C# working with windows forms. During the course of it, I did the following

        void HideButtons(object sender, EventArgs e)
    {
        Button hider = ((Button)sender);
        foreach(Button tohide in hider.Parent.Controls)
            tohide.Hide();
        hider.Show();
        hider.Text = "UnHide";
        hider.Click -= new EventHandler(HideButtons);
        hider.Click += new EventHandler(ShowButtons); 
    }



这段代码的目的是为了有一个按钮,隐藏所有其他按钮在容器它除了本身,然后变成一个取消隐藏按钮,做完全相反的事情。

The purpose of this code is to have a button which hides all the other buttons in the container it's in except itself, and then turn into an Unhide button which does the same thing in reverse.

现在,这一切都很好,但如我编译这个,我知道自己,我已经打了一个问题。藏起来是它的唯一的对象,是从((按钮)发送方)的回报。这并不一定是参考发件人,此代码可能会做任何事情。

Now, that's all well and good, except, as I compile this, I realize to myself I've hit a problem. hider is its unique object, being the return from ((Button)sender). It's not necessarily the reference to sender, and this code will probably do nothing.

但是低,你看,它的工作原理完全一样我把它想一开始还以为它会。这让我琢磨,并铸造总是返回到原来的对象的引用?如果不是,我怎么保证(按钮)发送=发送者?

But low and behold, it works exactly like I wanted it to and initially thought it would. Which got me to wondering, does a cast always return a reference to the original object? If not, how do I guarantee that (button)sender = sender?

我知道这不是双打/整数的情况下,为

I know that's not the case for doubles/ints, as

        public static int Main()
    {
        int a;
        double b;
        b = 10.5;
        a = (int)b;
        a++;
        return 0;
    }



用是11,和b是10.5但这可能是由于结束双打/整数是结构。这种行为令我很担心,这会是不错的知道,它总是会返回一个参考,所以我可以把我的脑海worrysome休息。

ends up with a being 11, and b being 10.5 But that may be due to doubles/ints being structs. This behavior worries me, and it'd be nice to know that it will always return a reference so I can put my worrysome mind to rest.

推荐答案

有关引用类型。如果转换仅仅是向上或向下的继承层次,然后肯定的。这是一个的引用转换的。从C#3.0语言规范,第6.2.4节:

For reference types. if the cast is just up or down the inheritance hierarchy, then yes. This is a reference conversion. From the C# 3.0 language spec, section 6.2.4:

引用转换,隐式或
明确的,永远不变的参考$其目标是
乙$ b的身份转换。换言之,虽然一个
引用转换可以改变参考的
型,它永远不会
改变
的对象的类型或值被称为

Reference conversions, implicit or explicit, never change the referential identity of the object being converted. In other words, while a reference conversion may change the type of the reference, it never changes the type or value of the object being referred to.

这是你用你的WinForms代码的情况。

This is the case you're using in your WinForms code.

不过,其他(还是引用类型)情况下,它可以调用的用户自定义转换的。例如:

However, in other (still reference type) cases it may invoke a user-defined conversion. For example:

using System;

class Foo
{
}

class Bar
{
    public static explicit operator Bar(Foo f)
    {
        return new Bar();
    }
}

class Test
{
    static void Main()
    {
        Foo f = new Foo();
        Bar b = (Bar) f;
        Console.WriteLine(object.ReferenceEquals(f, b)); // Prints False
    }
}



用户定义的转换是的相对的罕见的。

有关值类型,有装箱和拆箱转换,其他转换沿(如 INT之间双击)。

For value types, there are boxing and unboxing conversions, along with other conversions (e.g. between int and double).

这篇关于请问在C#中投对象总是引用返回到初始对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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