通过引用传递引用 vs 通过值传递引用 - C# [英] Pass reference by reference vs pass reference by value - C#

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

问题描述

您好,

我知道按值传递和按引用传递之间的区别.但是通过 ref 传递引用(例如数组)和通过值传递数组是我似乎无法理解的.如何通过引用传递引用?

 int[] myArray = {1,2,3};PassByVal(myArray);PassByRef(ref myArray);PassByVal(int[] 数组){ array = new int[] {7,8,9};//不管用 }PassByRef(ref int[] 数组){ array = new int[] {10,11,12};} //将工作

解决方案

我建议你查看 此链接.它非常有用并且包含关于C#中的参数传递的非常简单的示例.>

引用参数不传递函数成员调用中使用的变量的值 - 它们使用变量本身.不是在函数成员声明中为变量创建新的存储位置,而是使用相同的存储位置,因此函数成员中变量的值和引用参数的值将始终为相同.引用参数需要 ref 修饰符作为声明和调用的一部分——这意味着当你通过引用传递某些东西时总是很清楚的.看看我们之前的例子,只是把参数改成引用参数:

void Foo (ref StringBuilder x) {x = 空;}...StringBuilder y = new StringBuilder();y.Append("你好");富 (参考 y);Console.WriteLine (y==null);//将写入 TRUE

<小时>

在你的例子中

int[] myArray = {1,2,3};PassByVal(myArray);PassByRef(ref myArray);PassByVal(int[] 数组){//函数将指针的值复制到内存的新位置//复制"的指针仍然指向数组 123//现在你正在修改指针的拷贝所指向的对象//原来的指针仍然指向数组123//指针的副本将指向数组 456array = new int[] {7,8,9};}//不管用PassByRef(ref int[] 数组){//这里你传递的是指针而不在 a 中创建它的副本//内存的新位置//我们没有原始指针和复制"指针//我们只有原始指针,现在指向数组 10,11,12array = new int[] {10,11,12};} //将工作

Greetings,

I get the difference between pass by value and pass by reference. But pass reference (such as array) by ref and pass array by value is something i can't seem to comprehend. How can you pass a reference by reference?

     int[] myArray = {1,2,3};
     PassByVal(myArray);
     PassByRef(ref myArray);

     PassByVal(int[] array)
     {    array = new int[] {7,8,9};   // will not work }

     PassByRef(ref int[] array)
     {    array = new int[] {10,11,12}; }  // will work

解决方案

I suggest that you check out this link. It's quite useful and contains very simple examples about Parameter passing in C#.

Reference parameters don't pass the values of the variables used in the function member invocation - they use the variables themselves. Rather than creating a new storage location for the variable in the function member declaration, the same storage location is used, so the value of the variable in the function member and the value of the reference parameter will always be the same. Reference parameters need the ref modifier as part of both the declaration and the invocation - that means it's always clear when you're passing something by reference. Let's look at our previous examples, just changing the parameter to be a reference parameter:

void Foo (ref StringBuilder x) {
    x = null;
}

...

StringBuilder y = new StringBuilder();
y.Append ("hello");
Foo (ref y);
Console.WriteLine (y==null); // will write TRUE


IN YOUR EXAMPLE

int[] myArray = {1,2,3};
PassByVal(myArray);
PassByRef(ref myArray);

PassByVal(int[] array){
    // the function copy the value of the pointer in a new location of memory
    // the "copied" pointer still points to the array 123    

    // now you are modifying the object pointed by THE COPY of the pointer
    // the original pointer still points to array 123
    // the copy of the pointer will point to array 456
    array = new int[] {7,8,9}; 

} // will not work

PassByRef(ref int[] array){
   // here you are passing the pointer without creating a copy of it in a 
   // new location of memory

   // we have not a original pointer and a "copyed" pointer
   // we have only the original pointer and now whe point it to array 10,11,12
   array = new int[] {10,11,12}; 
}  // will work

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

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