如何在 C# 中通过 ref 传递它? [英] How to pass this by ref in C#?

查看:29
本文介绍了如何在 C# 中通过 ref 传递它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的一个类 (ClassA) 中,我想创建另一个类 (ClassB) 的相关实例,为它提供对启动它的创建的对象的引用.因此,我为 ClassB 提供了一个构造函数,该构造函数采用 (ref ClassB master) 参数.但是在 ClassA 中,我不能只调用 var slave = new ClassB(ref this).如何实施?

In a class (ClassA) of mine I want to create a related instance of another class (ClassB) providing it with a reference to the object who has initiated it's creation. So I've provided ClassB with a construcror taking a (ref ClassB master) argument. But in ClassA I can't just call var slave = new ClassB(ref this). How to implement this?

推荐答案

ref 关键字导致 传递引用语义——也就是说,如果变量在被调用的函数中被重新赋值,它会在被调用的函数中重新赋值变量来电者也是如此.

The ref keyword causes Pass by Reference semantics - that is, if the variable is re-assigned in the called function, it will re-assign the variable in the caller as well.

显然,这仅在 variable2(可以重新分配给)作为参数直接传递时才有效,如果任意表达式为通过了.在这种情况下,this 不是一个变量,而是一个不能重新赋值的特殊表达式,因此不能使用.

Obviously, this only works if a variable2 (which can be re-assigne to) is directly passed as the argument and will not work if an arbitrary expression is passed. In this case, this is not a variable, rather a special expression which cannot be re-assigned, and so cannot be used.

因此,这会起作用:(但请查看其他答案并继续阅读为什么这可能不需要和/或只是愚蠢的.)

As such, this would work: (But please see other answers and keep reading as to why this is likely not required and/or just silly.)

var me = this;
var slave = new ClassB(ref me);

但是,通过reference不应与混淆通过对象 [共享]1 语义传递.通过对象意味着如果一个对象被传递,那个对象被传递:该对象被复制/克隆/复制.如果对象发生变异,则对象发生变异.所有引用类型 在 C# 中具有传递对象语义,除非使用 refout.(class 关键字声明了一个新的引用类型,如帖子中的例子).

However, Pass by reference should not be confused with Pass by Object [Sharing]1 semantics. Pass by Object means that if an object is passed, that object is passed: the object is not copied/cloned/duplicated. If the object is mutated then the object is mutated. All Reference Types have Pass by Object semantics in C# unless either ref or out are used. (The class keyword declares a new reference type, as in the case in the post).

另一方面,值类型(例如struct),包括 intGuidKeyValuePair,有 传值语义 - 在这种情况下,复制,因此,如果值被修改,只有值结构(这是副本)更改.

On the other hand, Value Types (e.g. struct), including int and Guid and KeyValuePair<K,V>, have Pass by Value semantics - in this case a copy is made and thus, if the value is modified, only the value struct (which is a copy) changes.

快乐编码

1 在 C#/.NET 之下,通过按值传递对对象的引用来实现按对象传递.然而,上述规则正确地描述了可观察的语义和效果.

1 Underneath C#/.NET achieves Pass by Object by passing a reference to an object by Value. However, the rules above correctly describe the observable semantics and effects.

2 与 C# 只允许变量与 ref 一起使用不同,VB.NET 允许使用属性.VB.NET 编译器在编译过程中自动创建一个临时变量和属性的隐式读/写指令.

2 Unlike C#, which only allows variables to be used with ref, VB.NET allows Properties to be used. The VB.NET compiler automatically creates a temporary variable and implicit reading/writing instructions of the property during compilation.

这篇关于如何在 C# 中通过 ref 传递它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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