【发布时间】:2021-12-19 09:26:12
【问题描述】:
我收到错误“属性或索引器可能不会作为输出或引用参数传递” 我的任务是将冒泡排序实现为静态类,如果我将其设为非静态,它就可以正常工作。
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;
}
}
【问题讨论】:
-
(arr[j], arr[j + 1]) = (arr[j + 1], arr[j]); -
arr[j]是一个索引器(示例)。错误信息是正确的。您断言使其成为非静态会导致其工作是错误的:see here。