为什么'='不能在C#中超载? [英] Why can '=' not be overloaded in C#?

查看:121
本文介绍了为什么'='不能在C#中超载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在想,为什么我不能重载'='在C#中?我能得到一个更好的解释?

I was wondering, why can't I overload '=' in C#? Can I get a better explanation?

推荐答案

内存管理的语言通常引用,而不是对象。当你定义一个类及其成员正在定义的对象行为,但是当你创建你与那些对象的引用工作的变量。

Memory managed languages usually work with references rather than objects. When you define a class and its members you are defining the object behavior, but when you create a variable you are working with references to those objects.

现在,运营商=适用于引用,而不是对象。当你分配给另一个你实际上是使接收引用指向同一个对象,其他的引用是一个参考。

Now, the operator = is applied to references, not objects. When you assign a reference to another you are actually making the receiving reference point to the same object that the other reference is.

Type var1 = new Type();
Type var2 = new Type();

var2 = var1;

在上面,两个对象在堆上创建的代码,一个VAR1称为与另一个由VAR2。现在最后的语句,使VAR2参考点到VAR1是指同一个对象。该行后,垃圾收集器可以释放第二对象和只有一个在存储器对象。在整个过程中,没有操作应用于对象本身。

In the code above, two objects are created on the heap, one referred by var1 and the other by var2. Now the last statement makes the var2 reference point to the same object that var1 is referring. After that line, the garbage collector can free the second object and there is only one object in memory. In the whole process, no operation is applied to the objects themselves.

让我们回到为什么=不能超载,系统实现,你可以做的唯一的明智之举引用。可以超载被施加于对象的操作,而不是引用

Going back to why = cannot be overloaded, the system implementation is the only sensible thing you can do with references. You can overload operations that are applied to the objects, but not to references.

这篇关于为什么'='不能在C#中超载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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