小编N.K*_*sch的帖子

如何在C#中将数组作为未知类型的参数传递?

我想对整数或双精度数组进行排序.为此,我想使用一种方法.我的问题是我不知道如何传递一个未知类型的数组作为参数.我试过这个

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)

但是现在我不能使用大于运算符,因为数组也可以是字符串数组.

c# arrays sorting

1
推荐指数
1
解决办法
113
查看次数

如何同时为两个变量分配两个不同的值

我想同时为多个变量赋值.我知道它在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)

c#

0
推荐指数
1
解决办法
89
查看次数

标签 统计

c# ×2

arrays ×1

sorting ×1