我想对整数或双精度数组进行排序.为此,我想使用一种方法.我的问题是我不知道如何传递一个未知类型的数组作为参数.我试过这个
public static void BubbleSort<T>(T[] arr)
{
for (int i = 0; i < arr.Length; i++)
{
for (int j = 0; j < arr.Length - 1; j++)
{
//Can't use greater than because of T
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是现在我不能使用大于运算符,因为数组也可以是字符串数组.
我想同时为多个变量赋值.我知道它在python中如何工作但我需要一个C#的等价物.
我发现如果我想为新变量赋值,我可以这样做.
int a = 1 , b = 1;
//This works fine but now I need to set the variables again
a = b , b = a + b;
//But i can't do it like that
//I know that in python it would look like this (a , b = b , a+b)
Run Code Online (Sandbox Code Playgroud)