简体   繁体   中英

C# pass enum “name” and enum value to method

I'm doing Einstein-Riddle in C# atm and stubled across a problem. I'm trying to do it with enums. I have a struct called House. In House there are enums like Nationality, Color, Animal, ... I create every possible solution in House and want to delete things with the hints (in a separate method).

Hint #1: British lives in red house. Now I have to pass Nationality as enum and Nationality.British as value & Color as enum and Color.Red as value.

How to do this? I just want to call my Method like -

CheckLine(Nationality.British, Color.Red);

but what to put in parameter-list:

static void CheckLine(? )


Here's my struct and enums

public enum Nationalitaet { Däne, Brite, Deutscher, Norweger, Schwede };
   public enum Farbe { rot, blau, grün, weiß, gelb };
   public enum Zigarette { Dunhill, Marlboro, PallMall, Rothmans, Wingfield };
   public enum Tier { Pferd, Fisch, Katze, Vogel, Hund };
   public enum Getraenk { Wasser, Kaffee, Milch, Tee, Bier };

   struct Haus
   {
       public int HausNr { get; set; }
       public Nationalitaet Nationalitaet { get; set; }
       public Farbe Farbe { get; set; }
       public Zigarette Zigarette { get; set; }
       public Tier Tier { get; set; }
       public Getraenk Getraenk { get; set; }
   } 

I've a List with all solutions (called solution) in CheckLine there's

if (solution[i].Nationalitaet == Nationalitaet.British && solution[i].Farbe == Farbe.Red)       
        solution.RemoveAt(i)```

The correct signature would be:

static void CheckLine(Nationality nationality, Color color) { }

An enum type (eg Nationality ) can be used just like any other type, similar to how you would use int or string .

The names of the parameters ( nationality , color ) are of course free to choose, but generally speaking the most appropriate name for them matches the name of the enum type itself.

There is not enough information in the question to judge whether it should be static or not, or whether the method should or should not return a void . This answer only focuses on the input parameters of the method.

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