简体   繁体   English

通过引用C#调用“返回值”是否必要?

[英]C# call by reference “return value” is dispensable?

im confronted with some code and i wondered if there is a need for the return value in this method. 即时通讯面临一些代码,我想知道这种方法是否需要返回值。 the method changes some values of an two-dimensonal double array. 该方法更改了二维双精度数组的某些值。

public double[,] startsimplex_werte(double[,]f)
{
    double standardabstand_s = 1.0;

    double[] vektor_e1 = newdouble[2]{1.0,0.0};
    double[] vektor_e2 = newdouble[2]{0.0,1.0};
    f[1,1]=f[0,1]+vektor_e1[0]*standardabstand_s;
    f[1,2]=f[0,2];
    f[2,1]=f[0,1];
    f[2,2]=f[0,2]+vektor_e2[1]*standardabstand_s;

    return f;
}

theres an 2 dimensional array called: 二维数组称为:

double[,]f = new double[3,3];

so then the method is used on the array: 所以然后在数组上使用该方法:

f = berechne.startsimplex_werte(f);

its not important what the method does in detial. 该方法在实际操作中的作用并不重要。 my question is: 我的问题是:

is it necessary to return the values of the array by writing "return f " in the method. 是否有必要通过在方法中写入“ return f”来返回数组的值。 since arrays are reference types, there is a call by reference. 由于数组是引用类型,因此存在按引用的调用。 so the method already changes the actual data members of the array. 因此该方法已经更改了数组的实际数据成员。 i would have written the same method with no return value (void method) and just call the method without assigning it to the array. 我会写相同的方法,没有返回值(void方法),只调用该方法而不将其分配给数组。 imo its just important to transfer the array via parameter. imo通过参数传递数组只是重要的。 both variantes would do the same, or am I wrong? 两种变体都一样,还是我错了?

I hope somebody can tell me if my suggestion is true. 我希望有人能告诉我我的建议是否正确。

You're right, you don't need to return the array. 没错,您不需要返回数组。

You're modifying its content . 您正在修改其内容 If you were creating a whole new instance, then you'd have to either return it or pass the array using the ref / out keywords. 如果要创建一个全新的实例,则必须返回它或使用ref / out关键字传递数组。

You're right, in this case there's no need to return f as it is always the same as the input. 没错,在这种情况下,无需返回f因为它始终与输入相同。

There are a few reason why this might have been done: 这样做有几个原因:

  • It does allow the function to allocate a new array and return that, if necessary 它确实允许函数分配新数组并在必要时返回该数组
  • It's a more "functional" approach to API design 这是一种更加“实用”的API设计方法
  • If may be part of an API that uses the convention of taking an array and always returning one, and the author has chosen to follow this convention for consistency. 如果可能是API的一部分,该API使用采用数组并始终返回一个数组的约定,并且作者选择遵循此约定以保持一致性。

The problem is, that although it is call by reference, only a copy of that reference is passed to your method and not the original reference. 问题是,尽管它是通过引用调用的,但只有该引用的副本传递给了您的方法,而不是原始引用。

As long as you are modifing the content of the array both references are still pointing to the same object. 只要您正在修改数组的内容,两个引用仍然指向同一对象。 But if you assign a new array within the method this change would not affect the object outside of your method. 但是,如果在方法内分配新数组,则此更改不会影响方法外的对象。

So in general it is better to use the ref keyword in your method if you want to make sure that the orginial object reflects the changes or to use a return value of that method. 因此,通常,如果要确保原始对象能够反映更改或使用该方法的返回值,最好在方法中使用ref关键字。

But in this special case you don't need a return value or the keyword ref . 但是在这种特殊情况下,您不需要返回值或关键字ref But again, better use a return value or ref , depending on what you are doing. 但是同样,最好根据您的操作使用返回值或ref

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM