Instantiating all possible combinations of enums

2019-09-18 04:35发布

问题:

I'm having trouble iterating over all possible combinations of enums. I'm not too familiar with them as I've just started C# and am coming in from low level languages like C and assembler.

public enum enumA { A1, A2, A3 }
public enum enumB { B1, B2, B3 }

public class foo
{
    private enumA enumAfoo;
    private enumB enumBfoo;

    public foo()
    {
    }
    public foo(enumA A, enumB B)
    {
        enumAfoo = A;
        enumBfoo = B;
    }
}

public class fooTwo
{
    private List<foo> allCombinations = new List<foo>();
    public fooTwo()
    {

    }
    public List<foo> GetList()
    {
        return allCombinations;
    }
    public fooTwo(bool check)
    {
        if (check)
        {
            foreach (enumA i in Enum.GetValues(typeof(enumA)))
            {
                foreach (enumB j in Enum.GetValues(typeof(enumB)))
                {
                    allCombinations.Add(new foo(i, j));
                }
            }
        }
    }
}

When I run this code in a simple check output, I don't get what I'm after. Sample Main below. The output I get is just "TestingGround.foo" repeated 6 times, testing ground being my overall namespace. I Don't know if there's a problem with my logic of instantiating the list, or with how I'm converting it to string and outputting but I'd very much like some help in what's the correct procedure for doing this.

class Program
{
    static void Main(string[] args)
    {
        fooTwo A = new fooTwo(true);
        List<foo> list = A.GetList();
        foreach (foo j in list)
        {
            Console.WriteLine(j.ToString());
        }
        Console.ReadKey();
    }
}

回答1:

ToString() returns a default representation of the object. You need to create your own ToString() method, returning what you need. For example:

public class foo
{
    private enumA enumAfoo;
    private enumB enumBfoo;

    public foo()
    {
    }
    public foo(enumA A, enumB B)
    {
        enumAfoo = A;
        enumBfoo = B;
    }
    public override string ToString()
    {
        return enumAfoo.ToString() + "," + enumBfoo.ToString();
    }
}


回答2:

Please keep in mind that Enum has the Integer underlying value (btw, it must be unique: you can get it with simple (int) type casting). So, you are essentially have the values: 0, 1, 2 in each enum. Now, the unique combinations based on string concatenation are: 00, 01, 02, 10,11, 12, 20, 21, 22. Is it what you are looking for? Otherwise, you can apply "+" operator on underlying int values (but it will produce just 5 unique values).

Another scenario would be a concatenation of strings: you should use ToString() method in this case to get the combinations (assuming "+" operator applied to strings) of those "A1", "B1", etc. Also, you can use String.Concat(enum1, enum2) method as a shortcut; in this case you can omit the ToString()because String.Concat() will take care of proper type casting to string.

Hope this may help. Best regards,



标签: c# class enums