简体   繁体   English

如何通过枚举作为方法参数?

[英]How to pass enum as method parameter?

The task is to write a simple method that can sort int array (in ascending or descending order - should be set as enum type parameter of this method). 任务是编写一个可以对int数组进行排序的简单方法(按升序或降序-应该设置为此方法的枚举类型参数)。 I have written the method itself and enum, but I have no idea how to set enum as method parameter:( 我已经写了方法本身和枚举,但是我不知道如何将枚举设置为方法参数:(

Would be great to get any help from you, guys, cause I am completely new to coding. 伙计们,从您这里获得任何帮助将非常好,因为我对编码完全陌生。

class Program
{

    public enum options
    {
        UpSortOption,
        DownSortOption
    }

    public static void Main(string[] args)
    {
        int[] arr = new int[] { 3, 8, 0, 2, 16 };
    }

    static void orderArray(int [] array, options op)
    {
        switch(op)
        {
            case options.UpSortOption:
                Array.Sort(array);
                foreach (int number in array)
                {
                    Console.Write(number + " ");
                }
                break;

            case options.DownSortOption:
                Array.Sort(array);
                Array.Reverse(array);
                foreach (int number in array)
                {
                    Console.Write(number + " ");
                }
                break;

        }
    }
}

The signature of the method looks fine, Now you wanted to call this method by passing the first parameter of type integer array and the second parameter of type options for that you can use the following code: 该方法的签名看起来不错,现在您想要通过传递整数数组类型的第一个参数和类型options的第二个参数来调用此方法,可以使用以下代码:

orderArray(arr,options.UpSortOption);

Or else you can declare a variable of type options and pass that variable, the change you have to make for that case will be: 否则,您可以声明一个类型为options的变量并传递该变量,在这种情况下,您必须进行以下更改:

options optionsVariable = options.DownSortOption;
orderArray(arr,optionsVariable);

Let's take a step back to see if it helps your understanding. 让我们退后一步,看看它是否有助于您的理解。

If you have a method that takes a string and an int like this 如果您有一个采用字符串和int这样的方法

string MyMethod(string str, int num)
{
    // Do something
}

You'd use it like this 你会这样用

string rslt = MyMethod("Hello", 123);

What you've got here is something that takes in some stuff, does something to it, and gives you something in return. 您在这里所获得的是可以吸收某些东西,对其进行处理并为您提供回报的东西。 In this case MyMethod takes a string and an int , does something with them and returns a string which you then call rslt . 在这种情况下, MyMethod接受一个string和一个int ,对它们执行一些操作并返回一个string ,然后rslt string称为rslt

Your example follows the same basic pattern so you need to take your method - orderArray and give it the two things it wants - an int array and an option like this 您的示例遵循相同的基本模式,因此您需要采用方法orderArray并为其提供所需的两件事-一个int数组和一个类似这样的选项

int[] arr = new int[] { 3, 8, 0, 2, 16 };

orderArray(arr, options.UpSortOption);

Alternatively, you could create an options much like you'd create a string and then call your method like this 另外,您可以像创建一个字符串一样创建一个选项,然后像这样调用您的方法

int[] arr = new int[] { 3, 8, 0, 2, 16 };
options myOption = options.UpSortOption;

orderArray(arr, myOption);

To fully illustrate the point that an enum as a parameter isn't any different from say a string you could modify your method like this 为了充分说明枚举作为参数与说字符串没有什么不同,您可以像这样修改方法

static void orderArray(int[] array, string op)
{
    if (op == "UpSortOption")
    {
        Array.Sort(array);
        foreach (int number in array)
        {
            Console.Write(number + " ");
        }
    }
    else
    {
        Array.Sort(array);
        Array.Reverse(array);
        foreach (int number in array)
        {
            Console.Write(number + " ");
        }
    }
}

Then call it like this 然后这样称呼它

int[] arr = new int[] { 3, 8, 0, 2, 16 };
string myOption = "UpSortOption";

orderArray(arr, myOption);

This is how you pass it as a parameter 这是您将其作为参数传递的方式

orderArray(int [] array, typeof(options)){
    //Beep bap boop
}

Hope this aids you. 希望这对您有所帮助。

Exactly that's how you set up Enum as a method parameter. 正是这就是将Enum设置为方法参数的方式。

To call this method use: 要调用此方法,请使用:

orderArray(arr, options.UpSortOption);

You can also assign enum value to a variable and use this to call a method: 您还可以将枚举值分配给变量,然后使用它来调用方法:

options sortOpt = options.UpSortOption;
orderArray(arr, sortOpt);

You can also cast integer to an enum: 您还可以将整数转换为枚举:

orderArray(arr, (options)0);

OR

int opt = 0;
orderArray(arr, (options)opt);

Remembering, that if not specified otherwise, first element is 0, second is 1 and so on. 请记住,如果没有另外指定,则第一个元素为0,第二个元素为1,依此类推。

By the way, you should rather use PascalCase to name an enum type, like: 顺便说一句,您应该使用PascalCase来命名枚举类型,例如:

public enum Options
{ ... }

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

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