简体   繁体   English

在静态类方法中通过引用传递参数

[英]Passing arguments by reference in static class methods

I get the error "A property or indexer may not be passed as an out or ref parameter" My task is to implement buble sort as a static class if I make it non static it works just fine.我收到错误消息“属性或索引器可能无法作为输出或引用参数传递”我的任务是将 buble 排序实现为静态类,如果我将其设为非静态,则它工作得很好。

public static class BubleSort
    {
        public static void Sort(List<int> arr)
        {
            for (int i = 0; i < arr.Count-1; i++)
            {
                var flag = true;
                for (int j = 0; j < arr.Count-i-1; j++)
                {
                    if (arr[j] > arr[j + 1])
                    {
                        Swap(ref arr[j],ref arr[j + 1]);
                        flag = false;
                    }
                }

                if (flag)
                    break;
            }
        }

        private static void Swap(ref int v1,ref int v2)
        {
            int temp = v1;
            v1 = v2;
            v2 = temp;
        }
    }

You cant do what you are trying to do for exactly the reason the compiler says... However you could just use the reference of the list你不能完全按照编译器所说的原因做你想做的事情......但是你可以只使用列表引用

private static void Swap(IList<int> list, int i)
{
   int temp = list[i];
   list[i] =  list[i+1];
   list[i+1] = temp;
}

Or a 1 liner using tuple deconstruction或使用元组解构的 1 衬里

if (arr[j] > arr[j + 1])
{
   (arr[j], arr[j + 1]) = (arr[j + 1], arr[j]);
   flag = false;
}
Indexer access returns temporary value. 'ref' argument must be an assignable variable, field or an array element

You cannot send references from a list because access to specific index element is done through Indexer.您不能从列表发送引用,因为对特定索引元素的访问是通过索引器完成的。 see more about indexers 查看更多关于索引器

Instead, you can send references from an array int[] using ref, because it's not using indexer, but direct reference.相反,您可以使用 ref 从数组int[]发送引用,因为它不使用索引器,而是直接引用。

If you still want to use list, you can use C# feature to swap:如果你仍然想使用列表,你可以使用 C# 特性来交换:

(arr[j],arr[j + 1]) = (arr[j + 1], arr[j]);

instead of Swap method而不是Swap方法

Your code will become您的代码将成为

    public static class BubleSort
    {
        public static void Sort(List<int> arr)
        {
            for (int i = 0; i < arr.Count-1; i++)
            {
                var flag = true;
                for (int j = 0; j < arr.Count-i-1; j++)
                {
                    if (arr[j] > arr[j + 1])
                    {
                        (arr[j],arr[j + 1]) = (arr[j + 1], arr[j]);
                        flag = false;
                    }
                }

                if (flag)
                    break;
            }
        }
    }

There is a feature in the language for returning references.语言中有一个返回引用的功能。 But this is only implemented for arrays, not lists, since lists can be re-allocated.但这仅适用于数组,而不是列表,因为列表可以重新分配。 So I would expect your example code to work if you changed arr to an array.因此,如果您将arr更改为数组,我希望您的示例代码能够正常工作。 See for example this minimal example:例如,请参阅此最小示例:

        public void SwapTest()
        {
            var arr = new [] {1, 2};
            Swap( ref arr[0], ref arr[1]);
        }

        public static void Swap(ref int a, ref int b)
        {
            var tmp = a;
            a = b;
            b = tmp;
        }

However, for most practical applications I would suggest using one of the solutions posted by @TheGeneral.但是,对于大多数实际应用,我建议使用@TheGeneral 发布的解决方案之一。 ref returns are somewhat unusual, and may not be familiar to all programmers. ref 返回有点不寻常,可能不是所有程序员都熟悉。

In this case arr[j] is considered as a property, since it needs to read a field in the List.在这种情况下, arr[j] 被视为一个属性,因为它需要读取 List 中的一个字段。 Properties are not variables.属性不是变量。 They're methods, and cannot be passed to ref parameters.它们是方法,不能传递给 ref 参数。

If you want to do it this way, you would need to pass the array property to a temporary variable.如果你想这样做,你需要将数组属性传递给一个临时变量。

Example:例子:

if (arr[j] > arr[j + 1]){
   int tempJ = arr[j];
   int tempJ2 = arr[j + 1];
   Swap(ref tempJ, ref tempJ2);
   arr[j] = tempJ;
   arr[j + 1] = tempJ2;
   flag = false;
}

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

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