[英]C# WPF delegate return object
I have mainwindow, where is stored List<>
of some objects. 我有主窗口,其中存储了一些对象的List<>
。 Then user can open another window and I need to have access to List<>
from parent window in this new window, not just in constructor, but from any method etc... 然后用户可以打开另一个窗口,我需要在这个新窗口中从父窗口访问List<>
,不仅在构造函数中,而且从任何方法等都可以访问...
So I created public delegate with return type List<>
in child window. 因此,我在子窗口中创建了返回类型为List<>
公共委托。 And hooking delegate method in parent window like this: 并在父窗口中像这样钩住委托方法:
+=delegate{ return.List<>;}
Whenever I call this delegate in child window I get my List<>
. 每当我在子窗口中调用此委托时,我都会得到List<>
。
My question is, if List<>
is copied or if its reference to original one? 我的问题是,是否复制List<>
还是引用原始列表? I need it to be reference, so I dont waste memory. 我需要它作为参考,所以我不会浪费内存。
Thanks for answer! 感谢您的回答!
It will be returned as a reference to the original List<>. 它将作为对原始List <>的引用返回。 A List will need a very explicit Clone() to do otherwise. 列表将需要一个非常明确的Clone()来执行此操作。
Aside: Your public delegate should be an event
. 顺便说一句:您的公共代表应该是一个event
。
And with an interface or common base class, you could just have used a List<> property . 使用接口或通用基类,您可以只使用List <>属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.