两者有什么区别

引用传递的参数 参数通过value?

你能给我举几个例子吗?


当前回答

A major difference between them is that value-type variables store values, so specifying a value-type variable in a method call passes a copy of that variable's value to the method. Reference-type variables store references to objects, so specifying a reference-type variable as an argument passes the method a copy of the actual reference that refers to the object. Even though the reference itself is passed by value, the method can still use the reference it receives to interact with—and possibly modify—the original object. Similarly, when returning information from a method via a return statement, the method returns a copy of the value stored in a value-type variable or a copy of the reference stored in a reference-type variable. When a reference is returned, the calling method can use that reference to interact with the referenced object. So, in effect, objects are always passed by reference.

In c#, to pass a variable by reference so the called method can modify the variable's, C# provides keywords ref and out. Applying the ref keyword to a parameter declaration allows you to pass a variable to a method by reference—the called method will be able to modify the original variable in the caller. The ref keyword is used for variables that already have been initialized in the calling method. Normally, when a method call contains an uninitialized variable as an argument, the compiler generates an error. Preceding a parameter with keyword out creates an output parameter. This indicates to the compiler that the argument will be passed into the called method by reference and that the called method will assign a value to the original variable in the caller. If the method does not assign a value to the output parameter in every possible path of execution, the compiler generates an error. This also prevents the compiler from generating an error message for an uninitialized variable that is passed as an argument to a method. A method can return only one value to its caller via a return statement, but can return many values by specifying multiple output (ref and/or out) parameters.

请参阅c#讨论和示例链接文本

其他回答

如果你不想在将原始变量传递给函数后改变它的值,那么函数应该构造一个“按值传递”参数。

然后函数将只有值,而没有传入变量的地址。如果没有变量的地址,函数内部的代码就不能改变从函数外部看到的变量值。

但是如果你想要赋予函数从外部看到的改变变量值的能力,你需要使用引用传递。因为值和地址(引用)都是传递进来的,并且在函数内部可用。

这里有一个例子:

#include <iostream>

void by_val(int arg) { arg += 2; }
void by_ref(int&arg) { arg += 2; }

int main()
{
    int x = 0;
    by_val(x); std::cout << x << std::endl;  // prints 0
    by_ref(x); std::cout << x << std::endl;  // prints 2

    int y = 0;
    by_ref(y); std::cout << y << std::endl;  // prints 2
    by_val(y); std::cout << y << std::endl;  // prints 2
}

通过值传递-函数复制变量并使用副本(因此它不会改变原始变量中的任何内容)

引用传递——函数使用原始变量。如果你改变了另一个函数中的变量,它也会改变原来的变量。

示例(复制并使用/自己尝试一下):

#include <iostream>

using namespace std;

void funct1(int a) // Pass-by-value
{
    a = 6; // Now "a" is 6 only in funct1, but not in main or anywhere else
}

void funct2(int &a)  // Pass-by-reference
{
    a = 7; // Now "a" is 7 both in funct2, main and everywhere else it'll be used
}

int main()
{
    int a = 5;

    funct1(a);
    cout << endl << "A is currently " << a << endl << endl; // Will output 5
    funct2(a);
    cout << endl << "A is currently " << a << endl << endl; // Will output 7

    return 0;
}

保持简单,伙计们。成堆的文字是个坏习惯。

按值传递是指如何通过使用参数将值传递给函数。在按值传递中,我们复制存储在指定变量中的数据,并且它比按引用传递慢,因为数据是复制的。

或者我们对复制的数据进行更改。不影响原有数据。在按引用传递或按地址传递中,我们发送一个直接链接到变量本身。或者传递一个指向变量的指针。它更快是因为消耗的时间更少。

当通过引用传递时,基本上是传递一个指向变量的指针。通过值传递,即传递变量的副本。

在基本用法中,这通常意味着通过引用传递,对变量的更改将在调用方法中看到,而在通过值传递时则不会。