关于通过引用传递对象的问题 [英] A question about passing an object by reference

查看:52
本文介绍了关于通过引用传递对象的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我将一个类的引用传递给一个表单的构造函数,比如

so:public MyForm(int count,ref Area myArea){...}

如何在构造函数之外使用myArea?我应该创建另一个

区域并将myArea分配给它(即:Area foo = myArea;)还是有一个

更好的方式?


~AF

Hello all,

I''m passing a reference to a class into the constructor of a form, like
so: public MyForm(int count, ref Area myArea) {...}

How can I use myArea outside the constructor? Should I create another
Area and assign myArea to it (ie: Area foo = myArea;) or is there a
better way?

~AF

推荐答案

以下是我的写作方式:


private int _count;

private Area _myArea;


public MyForm(int count,Area myArea){_ count = count; _myArea =

myArea; }


//现在,您可以在任何方法中使用_myArea,例如:

public void DoSomethingWithMyArea(){_ myArea.DoSomething(); }


注意:构造函数中不需要ref关键字,因为对象是

总是通过引用传递在C#中。所以,使用上面的代码(没有ref

关键字),_ myArea和myArea指向同一个对象,这可能是你想要的




ref关键字实际上意味着输入/输出。这意味着调用者传递一个

对象并在调用完成时接收另一个对象(因此,你是
没有传递对象的引用,而是一个引用参考

(*))。它仅在方法需要返回多个结果时才有用。


(*)这是对真实图片的略微简化,因为在C#中

赋值新值的值由调用者而不是被调用者完成

(这样你就可以使用一对r / w访问器传递一个属性),所以如果你是

使用ref关键字,在

调用之前将值复制到临时变量,被调用者接收临时变量的地址,被调用者

写入其新值到临时变量,然后调用者在调用完成后将临时变量分配给参数(必须是

赋值的有效目标)。


Bruno。


" Abe Frohnman" <我们****** @ SPAMexperimentzero.org> écritdansle message de

news:sM ********************** @ news.easynews.com ...
Here is how I would write it:

private int _count;
private Area _myArea;

public MyForm(int count, Area myArea) { _count = count; _myArea =
myArea; }

// Now, you can use _myArea in any method, like:
public void DoSomethingWithMyArea() { _myArea.DoSomething(); }

Note: You do not need the ref keyword in the constructor because objects are
always passed "by reference" in C#. So, with the code above (no ref
keyword), _myArea and myArea point to the same object, which is probably
what you want.

The ref keyword really means in/out. It means that the caller passes one
object and receives a different object when the call completes (so, you are
not passing a reference to the object, but rather a reference to a reference
(*) ). It is only useful when a method needs to return several results.

(*) This is a slight simplification of the real picture because in C# the
assignment of the new value is done by the caller rather than by the callee
(so that you can pass a property with a pair of r/w accessors), so if you
use the ref keyword, the value is copied to a temporary variable before the
call, the callee receives the address of the temporary variable, the callee
writes its new value to the temporary variable, and then the caller assigns
the temporary variable to the argument (which must be a valid target for
assignment) after the call completes.

Bruno.

"Abe Frohnman" <us******@SPAMexperimentzero.org> a écrit dans le message de
news:sM**********************@news.easynews.com...
Hello all,

我正在将一个类的引用传递给一个表单的构造函数,比如
所以:public MyForm(int count,ref Area myArea ){...}

如何在构造函数外部使用myArea?我应该创建另一个
区域并将myArea分配给它(即:区域foo = myArea;)还是有更好的方法?

~AF
Hello all,

I''m passing a reference to a class into the constructor of a form, like
so: public MyForm(int count, ref Area myArea) {...}

How can I use myArea outside the constructor? Should I create another
Area and assign myArea to it (ie: Area foo = myArea;) or is there a
better way?

~AF



Bruno Jouhier [MVP]< bj ****** @ club-internet.fr>写道:
Bruno Jouhier [MVP] <bj******@club-internet.fr> wrote:
注意:在构造函数中不需要ref关键字,因为对象总是通过引用传递在C#。
Note: You do not need the ref keyword in the constructor because objects are
always passed "by reference" in C#.




不,他们不是。引用按值传递。我知道这可能比说完全解释它更容易

,但我认为它确实会对

结束造成伤害。


致OP:请参阅 http://www.pobox

关于通过引用传递的内容的完整讨论.com / ~sipet / csharp / parameters.html
真的意思是,和b $ b一样,C#做什么。


-

Jon Skeet - < sk *** @ pobox。 com>
http://www.pobox.com/~skeet/

如果回复小组,请不要给我发邮件



No they''re not. References are passed by value. I know it may be easier
to say this than to explain it fully, but I think it does harm in the
end.

To the OP: see http://www.pobox.com/~skeet/csharp/parameters.html for a
full discussion about what "passing by reference" really means, and
what C# does.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too


嗯...所以C#以不同于C ++的方式传递对象?我知道,如果你没有明确地告诉C ++函数/方法该对象将通过引用传入
,它会被值传入。所以只是为了确保我正确理解这一点,每当我将一个物体传递给

函数/方法时,它将>总是<通过引用传递?我在方法中对该对象所做的任何

更改都会影响该方法之外的对象




AF


Bruno Jouhier [MVP]写道:
Hmm...so C# passes objects differently than C++ does? I know that if you
don''t explicitly tell a C++ function/method that the object is going to
be passed in by reference, it gets passed in by value. So just to ensure
that I''m understanding this correctly, whenever I pass an object into a
function/method it''ll >always< be passed in by reference? And any
changes I make to that object in the method will affect the object
outside of that method?

AF

Bruno Jouhier [MVP] wrote:
这是我怎么写的:

private int _count;
私有区_myArea;

公共MyForm(int count,Area myArea){_ count = count; _myArea =
myArea; }

//现在,您可以在任何方法中使用_myArea,例如:
public void DoSomethingWithMyArea(){_ myArea.DoSomething();注意:在构造函数中不需要ref关键字,因为对象总是通过引用传递。在C#中。所以,使用上面的代码(没有ref
关键字),_ myArea和myArea指向同一个对象,这可能是你想要的。

ref关键字真正意味着输入/输出。这意味着调用者传递一个
对象并在调用完成时接收不同的对象(因此,您没有传递对该对象的引用,而是对引用的引用
( *))。它仅在方法需要返回多个结果时才有用。

(*)这是对真实图像的略微简化,因为在C#中,新值的分配是由调用者而不是被调用者
(这样你就可以使用一对r / w访问器传递一个属性),所以如果你使用ref关键字,那么该值将被复制到临时变量之前
调用,被调用者接收临时变量的地址,被调用者将其新值写入临时变量,然后调用者将临时变量分配给参数(必须是呼叫完成后,
分配的有效目标。
Here is how I would write it:

private int _count;
private Area _myArea;

public MyForm(int count, Area myArea) { _count = count; _myArea =
myArea; }

// Now, you can use _myArea in any method, like:
public void DoSomethingWithMyArea() { _myArea.DoSomething(); }

Note: You do not need the ref keyword in the constructor because objects are
always passed "by reference" in C#. So, with the code above (no ref
keyword), _myArea and myArea point to the same object, which is probably
what you want.

The ref keyword really means in/out. It means that the caller passes one
object and receives a different object when the call completes (so, you are
not passing a reference to the object, but rather a reference to a reference
(*) ). It is only useful when a method needs to return several results.

(*) This is a slight simplification of the real picture because in C# the
assignment of the new value is done by the caller rather than by the callee
(so that you can pass a property with a pair of r/w accessors), so if you
use the ref keyword, the value is copied to a temporary variable before the
call, the callee receives the address of the temporary variable, the callee
writes its new value to the temporary variable, and then the caller assigns
the temporary variable to the argument (which must be a valid target for
assignment) after the call completes.






这篇关于关于通过引用传递对象的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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