通过引用传递争论 [英] Passing arguements by reference

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

问题描述




我有两个班,A和B,


B在构造函数中将A作为参数:

A a1 =新A();

B b =新B(a1);


///


A a2;

公共B(A aa)

{

a2 = aa;

}


但是,我认为我可以通过引用传递aa来减少内存使用并增加

的性能。 br />

如何改变我的代码才能做到这一点?


我知道我需要这样做:


public B(ref A aa)


但我如何声明a2?如果我原样保留它,这是否意味着a1和

a2都指向同一个内存位置,或者分配a2时是否复制了

对象?

谢谢


Andrew

解决方案

" Andrew Bullock" <一个********************* @ ANDntlworldTHIS.com>在消息中写道

新闻:oI ******************* @ newsfe4-gui.ntli.net ...



我有两个班级,A和B,

B在其构造函数中使用A作为参数:

A a1 =新A();
B b =新B(a1);

///

A a2;
公共B(A aa )
{
a2 = aa;
}

然而,我认为我可以通过传递aa来减少内存使用并提高性能/> reference。




这是一个不正确的假设。

在上面的代码中,您已经传递了对A对象的引用在B#的构造函数中。

在C#中,你永远不会直接传递对象作为参数,而是传递对象的引用。

因此,在B类中,你只需支付参考费用。

净效果是代码中的a1和a2都是对同一个A对象的引用。


如果你不喜欢我不想分享这个对象,然后你需要克隆它并保存对

克隆的引用。

当然,既然你提到减少内存使用量,这显然不是一个问题,在这种情况下


希望这有帮助


比尔


< snip>


但是,我认为通过
引用传递aa可以减少内存使用并提高性能。


这将是一个不正确的假设。
在上面的代码中,您已经将对A的引用传递给B的构造函数。
在C#中,你永远不会直接将对象作为参数传递,而是将引用传递给对象。
因此,在B类中,您只需支付引用的成本。
净效应是代码中的a1和a2都是是对同一个A对象的引用。

如果您不想共享该对象,那么您需要克隆它并且sa请参考
克隆。
当然,既然你提到减少内存使用量,这在这种情况下显然不是问题

希望这有帮助
<比尔

< snip>






所以ref关键字只是为了传递价值变量参考?

谢谢


Andrew


嗨安德鲁,

在您的示例中,即使不使用ref关键字,您也已经通过引用传递了A的实例

。有很多方法可以通过

值:


1.按值传递值类型

2.传递通过引用的值类型

3.按值传递引用类型

4.通过引用传递引用类型


In .Net有两种主要类型的值类型和引用类型。价值

类型是保存他们所引用物品实际价值的变量,所以

例如:


int i = 3;


i -------存储在------>内存地址1234567,其值为3


以上真的意味着我正在引用一个存储值为

3的内存位置。

引用类型是一个不保存实际数据的变量,但

指向存储实际值的内存位置,即


人p1 =新人(bob);

人p2 = p2;


p1 ---存储在------> ;地址12345:包含内存地址67890

p2 ---存储在------>地址12134:包含内存地址67890

内存地址67890存储名为Bob的人的实际数据。


即使你有两个不同的变量它们都引用了

相同的对象。


当你传递值类型时,它们默认被复制,所以将int传递给

a函数会导致值类型被复制,它是您在函数内访问的int

的副本。如果您在

上使用ref关键字,则不会生成副本,并且将对同一个int的引用传递给函数,例如:


使用System;

使用System.Text;


命名空间ConsoleApplication19

{

class program

{

static void Main(string [] args)

{

//值类型示例

int j = 1;

AddOne(j);


// j仍然是1

Console.WriteLine("按值传递值:j在AddOne =" + j之后);


int k = 1;

AddOne(参考k);


// k现在将是2

Console.WriteLine("以ref:k传递值)在AddOne =" + k)之后;


Console.ReadLine();

}


// /< summary>

///按值传递值类型

///< / summary>

static void AddOne(int i)

{

i = i + 1;

}


///< summary>

///通过引用传递值类型

// /< / summary>

static void AddOne(ref int i)

{

i = i + 1;

}

}

}

当您将引用传递给
$ b $时,现在类是默认引用类型ba函数作为参数没有制作对象的副本,但是(一个微妙的

指向注释)引用该对象的引用被复制如果ref

不使用关键字。例如:


void DoSomething(Person q)

{

//更改此人的姓名

q.Name =" Frank";


//让q指向任何东西

q = null;

}


void Main()

{

人p =新人();

p.Name =" Bob";


DoSomething(p);


//此时p.Name ==" ; Frank"

//而p不为空

}


变量p和q仍然指向同一个对象但是当p被传递给函数的时候,传递给p函数的一个副本,所以当我们将q设置为

null时,我们没有将p设置为null,因为它们是不同的变量。


如果我们想要q和p是指向同一个对象的同一变量,我们可以在ref对象上使用ref关键字,即


无效DoSomething(参考人q)

{

q.Name =" John&quo t ;;

q = null;

}


void Main()

{

Person p = new Person();

p.Name =" Mark";


DoSomething(ref p);


//这里p为null,因为DoSomething方法。

}

希望这有点道理。


Mark
http://www.markdawson.org

" Andrew Bullock"写道:



我有两个班,A和B,

B以A作为参数构造函数:

A a1 = new A();
B b = new B(a1);

///

A2;
公共B(A aa)
{
a2 = aa;
}

然而,我认为我可以通过引用传递来减少内存使用并提高性能。

如何更改代码来执行此操作?

我知道我需要这样做:

公共B(参考A aa)

但我如何宣布a2?如果我原样保留它,是否意味着a1和
a2都指向同一个内存位置,或者当分配a2时是复制了
对象?

谢谢

Andrew



Hi,

I have two classes, A and B,

B takes an A as an argument in its constructor:
A a1 = new A();
B b = new B(a1);

///

A a2;
public B(A aa)
{
a2 = aa;
}


However, I figured that I could reduce memory usage and increase
performance by passing aa by reference.

How do I alter my code to do this?

I know I need to do this:

public B(ref A aa)

but how do i declare a2? If i leave it as it is, does that mean a1 and
a2 both point to the same memory location, or when assigning a2 is the
object copied?
Thanks

Andrew

解决方案

"Andrew Bullock" <an*********************@ANDntlworldTHIS.com> wrote in message
news:oI*******************@newsfe4-gui.ntli.net...

Hi,

I have two classes, A and B,

B takes an A as an argument in its constructor:
A a1 = new A();
B b = new B(a1);

///

A a2;
public B(A aa)
{
a2 = aa;
}


However, I figured that I could reduce memory usage and increase performance by passing aa by
reference.



That would be an incorrect assumption.
In the code above, you are already passing a reference to an A object in to the constructor for B.
In C#, you never directly pass objects as parameter, instead you pass references to objects.
Thus, in class B, you only pay for the cost of a reference.
The net effect is that a1 and a2 in your code both are references to the same A object.

If you don''t wish to share the object, then you need to clone it and save the reference to the
clone.
Of course, since you mentioned reducing memory usage, this is obviously not a problem in this case

Hope this helps

Bill

<snip>


However, I figured that I could reduce memory usage and increase performance by passing aa by
reference.



That would be an incorrect assumption.
In the code above, you are already passing a reference to an A object in to the constructor for B.
In C#, you never directly pass objects as parameter, instead you pass references to objects.
Thus, in class B, you only pay for the cost of a reference.
The net effect is that a1 and a2 in your code both are references to the same A object.

If you don''t wish to share the object, then you need to clone it and save the reference to the
clone.
Of course, since you mentioned reducing memory usage, this is obviously not a problem in this case

Hope this helps

Bill

<snip>


Hi,

So is the ref keyword just for passing value variables by reference?
Thanks

Andrew


Hi Andrew,
in your example you are already passing the instance of A by reference
even without using the ref keyword. There are a number of ways you can pass
values:

1. Passing a value type by value
2. Passing a value type by reference
3. Passing a reference type by value
4. Passing a reference type by reference

In .Net there are two main types value types and reference types. Value
types are variables which hold the actual value of the item they refer to, so
for example:

int i = 3;

i ------- stored at ------> Memory Address 1234567 which holds value 3

so above really means i is refering to a memory location in which the value
3 is stored.
A reference type is a variable which does not hold the actual data, but
points to a memory location that stores the actual value i.e.

Person p1 = new Person("bob");
Person p2 = p2;

p1 ---stored in ------> Address 12345: contains memory address 67890
p2 ---stored in ------> Address 12134: contains memory address 67890

Memory address 67890 stores the actual data for the Person called Bob.

So even though you have two different variable they are both referencing the
same object.

When you pass value types they by default get copied, so passing an int into
a function causes the value type to be copied and it is the copy of the int
which you are accessing inside the function. If you use the ref keyword on
the int then the copy is not made and a reference to the same int is passed
into the function, for example:

using System;
using System.Text;

namespace ConsoleApplication19
{
class Program
{
static void Main(string[] args)
{
// Value Type Examples
int j = 1;
AddOne(j);

//j will still be 1
Console.WriteLine("passing value by value: j after AddOne=" + j);

int k = 1;
AddOne(ref k);

// k will now be 2
Console.WriteLine("passing value by ref: k after AddOne=" + k);

Console.ReadLine();
}

/// <summary>
/// Passing Value type by value
/// </summary>
static void AddOne(int i)
{
i = i + 1;
}

/// <summary>
/// Passing value type by reference
/// </summary>
static void AddOne(ref int i)
{
i = i + 1;
}
}
}
Now classes are by default reference types, when you pass a reference into
a function as a parameter a copy of the object is not made, but (a subtle
point to note) the reference that refers to the object is copied if the ref
keyword is not used. For example:

void DoSomething(Person q)
{
//Change the name of the person
q.Name = "Frank";

//make q point to nothing
q = null;
}

void Main()
{
Person p = new Person();
p.Name = "Bob";

DoSomething(p);

//at this point p.Name == "Frank"
//and p is not null
}

The variables p and q were still pointing to the same object but when p was
passed to the function a copy of the p variable was made so when we set q to
null we were not setting p to null because they were different variables.

If we want q and p to be the same variable pointing to the same object we
can use the ref keyword on the ref object i.e.

void DoSomething(ref Person q)
{
q.Name = "John";
q = null;
}

void Main()
{
Person p = new Person();
p.Name = "Mark";

DoSomething(ref p);

//here p is null, because of the DoSomething method.
}
Hope that makes some sense.

Mark
http://www.markdawson.org
"Andrew Bullock" wrote:

Hi,

I have two classes, A and B,

B takes an A as an argument in its constructor:
A a1 = new A();
B b = new B(a1);

///

A a2;
public B(A aa)
{
a2 = aa;
}


However, I figured that I could reduce memory usage and increase
performance by passing aa by reference.

How do I alter my code to do this?

I know I need to do this:

public B(ref A aa)

but how do i declare a2? If i leave it as it is, does that mean a1 and
a2 both point to the same memory location, or when assigning a2 is the
object copied?
Thanks

Andrew



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

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