c# - 按值(value)传递?

标签 c# function copy pass-by-value

我在传递 C# 函数参数时遇到问题。

我想知道如何让 C# 函数按值接受参数(以复制原始对象)。

我认为这是 C# 处理这些事情的默认方式,但在下面的代码中:

using System;
using System.Collections.Generic;
using System.Linq;
class MaximumElement
{
    static void Main(string[] args)
    {
        Stack<int> numbers = new Stack<int>();
        int n = int.Parse(Console.ReadLine());
        for (int i = 0; i < n; i++)
        {
            string input = Console.ReadLine();
            switch (input)
            {
                case "2": numbers.Pop(); break;
                case "3": Console.WriteLine(maxElement(numbers)); break;
                default:
                    string[] argz = input.Split(' ');
                    numbers.Push(int.Parse(argz[1]));
                    break;
            }
        }
    }


    public static int maxElement(Stack<int> stack)
    {
        int max = stack.Peek();
        for (int i = 0; i < stack.Count; i++)
        {
            if (max >= stack.Peek())
            {
                stack.Pop();
            }
            else if (max < stack.Peek())
            {
                max = stack.Pop();
            }
        }
        return max;
    }
}

我的 maxElement() 函数实际上更改了我传递给它的原始堆栈,而绕过它的唯一方法是手动复制我的堆栈传递给函数内部的函数。 感谢您提前回复:)

最佳答案

不要将通过引用 传递的参数与值类型引用类型 混合使用。这是初学者常犯的错误,您需要清楚地了解这两者虽然在某种程度上相关,但却是语言的完全不同的特性。

我可能不会使用精确的术语,因为英语不是我的语言,但我希望我能理解这个想法:

  • 值类型:变量是值本身。当您编写以下内容时:int i = 1; 变量 i 保存值 1
  • 引用类型:变量是指向内存中对象所在位置的引用。这意味着,当您说 string s = "Hello"; s 不包含 "Hello" 时,它包含内存地址,其中 “你好”被存储。

那么当您按值传递参数时会发生什么(C# 中的默认设置)。我们有两种可能性:

  • 参数是一个值类型:你得到一个变量的副本,这意味着 如果你传递 i = 1 你会收到一个 copy 这也 包含 1,但两者 都是不同的对象。

    这在处理可变值类型时很明显,例如System.Drawing.Point:

    Point point = new Point(0, 0);
    
    Frob(point);
    var b = point.X == 1 && point.Y == 1; //False, point does not change.
    
    void Frob(Point p) { p.Offset(1, 1); } // p is a copy of point and therefore contains a copy of the value stored in point, not the value itself.
    
  • 参数是引用类型:您获得了变量的副本,这意味着您获得了对内存地址的引用的副本,但是副本指向的对象 < em>是一样的。这就是您所处的场景。

    Foo foo = new Foo();
    foo.Blah = 1;
    
    Frob(foo);
    var b = foo.Blah == 2; //True, foo.Blah has been modified.
    
    void Frob(Foo f) { foo.Blah = 2; } //both foo and f point to the same object.
    

    请注意,在这两种情况下,您不能做的是修改引用指向的内容。这行不通:

    string s = "hello";
    foo(s);
    var b = s == "bye"; //false, s still points to the original string
    
    void Foo(string str)
    {
        str = "bye";
    }
    

现在,如果我们通过引用传递会发生什么?好吧,主要区别在于您传递的是变量本身,而不是副本。这意味着在值类型的情况下,您传递的是原始值,而在引用类型的情况下,您传递的是原始地址,而不是副本。这允许:

    //Value type
    Point point = new Point(0, 0);

    Frob(ref point);
    var b = point.X == 1 && point.Y == 1; //True, point and p are the same variable.

    void Frob(ref Point p) { p.Offset(1, 1); }

    //Value or reference type
    string s = "hello";
    foo(ref s);
    var b = s == "bye"; //true

    void Foo(ref string str)
    {
        str = "bye";
    }

希望这能澄清区别。

关于c# - 按值(value)传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37279581/

相关文章:

c# - HttpWebResponse 返回远程服务器返回错误 : (403) Forbidden

c# - 从 XML 填充对象

c++ - 广义 lambda 的 vector

vba - PowerPoint VBA : Copy and paste image, 对齐到中心,并拉伸(stretch)以适合页面

gradle - 如何通过groovy将文件复制到目录(如果不存在)(如果存在则不复制)

c# - LambdaExpression 构造函数

C# URL 的最大长度

function - 如何避免将大量参数传递给嵌套函数?

javascript - 从传递给函数的字符串调用 javascript 函数

php - 通过 HTTPS 从远程服务器复制图像