简体   繁体   中英

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). 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:

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 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

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 .

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

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.

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.

By the way, you should rather use PascalCase to name an enum type, like:

public enum Options
{ ... }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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