C# 正在尝试从program.cs调用类

C# 正在尝试从program.cs调用类,c#,visual-studio-2010,quicksort,C#,Visual Studio 2010,Quicksort,我创建了一个类来对整数数组进行快速排序,现在我试图从程序中调用该类,但我不断收到一个错误,该类如下所示: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace QuicksortSimpel { class Class1 { public static void Quicksort(IComparable[] elements

我创建了一个类来对整数数组进行快速排序,现在我试图从程序中调用该类,但我不断收到一个错误,该类如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QuicksortSimpel
{
    class Class1
    {
        public static void Quicksort(IComparable[] elements,int left, int right)
        {

            //Define i and j and pick an pivot to compare other numbers
            int i = left, j = right;
            IComparable pivot = elements[left + (left + right) / 2];

            //Compare numbers and then compare to pivot
            while (i <= j)
            {
                while (elements[i].CompareTo(pivot) < 0)
                {
                    i++;
                }

                while (elements[j].CompareTo(pivot) > 0)
                {
                    j--;
                }

                if (i <= j)
                {
                    // Switch
                    IComparable tmp = elements[i];
                    elements[i] = elements[j];
                    elements[j] = tmp;

                    i++;
                    j--;
                }
            }
            // repeat
            if (left < j)
            {
                Quicksort(elements, left, j);
            }

            if (i < right)
            {
                Quicksort(elements, i, right);
            }
        }
    }
}
我得到的错误是:


方法quicksort不重载1个参数。

这是因为
Class1.quicksort()接受三个参数,您将传递1。而且这样做时它也不会返回任何内容
numbers=Class1.Quicksort()将不起作用

您可以重载或设置可选/默认参数(谢谢)


这是因为
Class1.Quicksort()接受三个参数,您将传递1。而且这样做时它也不会返回任何内容
numbers=Class1.Quicksort()将不起作用

您可以重载或设置可选/默认参数(谢谢)


出现此编译器错误是因为类方法QuickStart()需要三个参数,而您只提供了一个参数


您的类不是静态的,所以为什么要用类名访问它??您需要创建
Class1
的对象,然后对该对象调用sort方法。我还注意到,您正在将此方法返回的值赋给某个变量,但在函数定义中,您没有返回任何内容???

您将收到此编译器错误,因为类方法QuickStart()需要三个参数,而您只提供了一个参数


您的类不是静态的,所以为什么要用类名访问它??您需要创建
Class1
的对象,然后对该对象调用sort方法。我还注意到,您正在将此方法返回的值分配给某个变量,但在函数定义中,您没有返回任何内容???

错误消息不清楚?可能使用了
参数
?错误消息不清楚?可能使用
params
?这肯定是错误的原因。如果希望能够传递单个参数,请为左值和右值设置默认值(或创建重载)。例如:
快速排序(IComparable[]元素,int left=0,int right=0)
。另外,我强烈建议使用
IEnumerable
而不是数组以获得更大的灵活性(您的返回值也将是排序后的
IEnumerable
)。好的,因此当我传递3个参数(需要排序的数组)和两个整数时,我将得到另一个错误:“QuicksortSimpel.Class1.Quicksort”的最佳重载方法匹配(System.IComparable[],int,int)“”具有一些无效参数,无法从“int[]”转换为“System.IComparable[]”这在我放置(IComparable[]元素时也会发生,int left=0,int right=0)在class@D3l4n0这可能是因为您传递的数组不是
系统的数组。IComparable
,您确定数组中的对象就是这样吗?@SiLo我在我的答案中加入了您的建议,并添加了重载选项。谢谢。@D3l4n0如果我的答案对您有帮助,您能接受吗?谢谢!这是de错误的原因有限。如果您希望能够传递单个参数,请为左值和右值设置默认值(或创建重载)。例如:
QuickSort(IComparable[]元素,int left=0,int right=0)
。此外,我强烈建议使用
IEnumerable
而不是数组,以获得更大的灵活性(您的返回值也将是排序后的
IEnumerable
)。因此,当我传递3个参数(需要排序的数组)和两个int时,我将得到另一个错误:与“QuicksortSimpel.Class1.Quicksort(System.IComparable[],int,int)”匹配的最佳重载方法具有一些无效参数,无法从“int[]转换“to”System.IComparable[]”当我放置(IComparable[]元素,int left=0,int right=0)时也会发生这种情况在class@D3l4n0这可能是因为您传递的数组不是
系统的数组。IComparable
,您确定数组中的对象就是这样吗?@SiLo我在我的答案中加入了您的建议,并添加了重载选项。谢谢。@D3l4n0如果我的答案对您有帮助,您能接受吗?谢谢!
numbers = Class1.Quicksort(numbers);
public static void Quicksort(IComparable[] elements,int left = 0, int right = 0)
public static void Quicksort(IComparable[] elements)
{
    QuickSort(elements, 0,0);
}