我应该如何按值传递一个数组?是不是克隆()泄气? [英] How should I pass an array by value? Isn't Clone() discouraged?

查看:106
本文介绍了我应该如何按值传递一个数组?是不是克隆()泄气?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在调试我的C#程序,经过时间的几个跨度无果而终,发现从基准而不是按值不小心传递数组附带的错误。

下面是一些伪code:

  //一号通
currentVertices =水平[0] .vertices;
currentVertices [0] .Position.Z - = 10;//第2次
currentVertices =水平[1] .vertices;
currentVertices [0] .Position.Z - = 10;// 3通
currentVertices =水平[0] .vertices;
currentVertices [0] .Position.Z - = 10;//currentVertices[0].Position.Z现在是10低于预期;
// 20不到就开始了。进一步的循环会增加差异预期
//值。

我不期待已通过引用数组,但我不小心这样做呢。

有没有办法通过的指定一个数组值的而不是按引用?我知道我可以通过两个数组迭代,单独把每个术语,但似乎过于复杂。我有理由相信,克隆()函数,那么,克隆的值没有相应的引用,但是,只有这样,才能通过赋值?即 currentVertices =水平[1] .vertices.Clone();

是不是克隆()通常不鼓励?什么是对按值传递数组的最佳实践?

在短期内,我已经根本就很难codeD应该被加载后什么值。

由于我主持的一个朋友说,


  

我看你解决了通过绕过引用传递通过引用。



解决方案

好了,在C#中只值类型按值传递。 (这是一个过于简单化,但就是看它一个体面的方式。)其他每一件事实际上是受价值,这是类似于传递指针传递的参考。 (请注意,这是通过被裁判不同,这将允许在调用方法变量的更新。)

您有两种选择:


  1. 重构code,以避免在数组中变异的对象

  2. 深克隆阵列

这些都是大致的顺序,我会推荐。

I've been debugging a C# program of mine, and after several fruitless spans of time, discovered the error came from "accidentally" passing an array by reference instead of by value.

Here's some pseudocode:

//1st pass
currentVertices = levels[0].vertices;   
currentVertices[0].Position.Z -= 10;

//2nd pass
currentVertices = levels[1].vertices;
currentVertices[0].Position.Z -= 10;

//3rd pass
currentVertices = levels[0].vertices;
currentVertices[0].Position.Z -= 10;  

//currentVertices[0].Position.Z is now 10 less than expected;
//20 less than it started. Further loops will increase the difference in expected
//value.

I was not expecting to have passed the Array by reference, but I accidentally did so anyway.

Is there a way to assign an array by value instead of by reference? I know that I could iterate through both arrays, assigning each term individually, but that seems unnecessarily complex. I'm reasonably sure that the Clone() function, well, clones, the values without the respective references, but is that the only way to assign by value? I.e. currentVertices = levels[1].vertices.Clone();

Isn't Clone() generally discouraged? What's the best practice for passing arrays by value?

In the short term, I've simply hardcoded what the values should be after being loaded.

As one of my moderator friends remarked,

"I see you solved pass-by-reference through by-pass-reference."

解决方案

Well, in C# only value types are passed "by value". (This is an oversimplification, but that is a decent way to look at it.) Every thing else is actually a reference that is passed by value, which is similar to passing a pointer. (Note that this is different to passing "by ref", which would allow the update of a variable in a calling method.)

You have two options:

  1. refactor your code to avoid mutating the objects in the array
  2. deep-clone the array

Those are roughly in the order that I would recommend.

这篇关于我应该如何按值传递一个数组?是不是克隆()泄气?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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