简体   繁体   中英

How to add multiple key-value pairs to an array of dictionaries (or similar solution)?

I am searching for a cleaner way of writing an array of dictionaries in C# (or an alternative), when all of them have the same key-value pairs (only the values differ).

I need to create 3 power plant objects, and all 3 of them need to have different values for the attributes "Dam", "Diversion" and "First", and I am using the different values of the attributes later in the code. I am fairly new in writing code in C#, so when I searched for the best way to solve this problem, I came across dictionaries and decided to use them for it. So far I have created an array of 3 dictionaries in the Program class, and then added the required values in the Main method.

static Dictionary<string, int>[] power_plant = new Dictionary<string, int>[]
         {
            new Dictionary<string, int>(),
            new Dictionary<string, int>(),
            new Dictionary<string, int>()
         };

for (int i = 0; i < 3; i++) 
{
   if (i == 0)
   {
      power_plant[i].Add("Dam", 1);
      power_plant[i].Add("Diversion", 0);
      power_plant[i].Add("First", 1);
   }
   else if (i==1)
   {
      power_plant[i].Add("Dam", 1);
      power_plant[i].Add("Diversion", 0);
      power_plant[i].Add("First", 0);
   }
   else
   {
      power_plant[i].Add("Dam", 0);
      power_plant[i].Add("Diversion", 1);
      power_plant[i].Add("First", 0);
   }
}

Although, the code works properly, I feel that there has to be a better way to achieve what I need, whether it is a cleaner code using dictionaries or using entirely different types. However, I do not have the required knowledge to think of an alternative.

Any help would be appreciated.

welcome to the C# world! I am not sure what are you trying to achieve, but it seems like you are over complicating your work. Check this:

I need to create 3 power plant objects, and all 3 of them need to have different values for the attributes "Dam", "Diversion" and "First"

In that case, create a PowerPlant class:

public class PowerPlant
{
    public PowerPlant(int dam, int diversion, int first)
    {
        Dam = dam;
        Diversion = diversion;
        First = first;
    }

    public int Dam { get; }
    public int Diversion { get; }
    public int First { get; }
}

Then just add it to a list:

var PowerPlants = new List<PowerPlant>
{
    new PowerPlant(1, 0, 1),
    new PowerPlant(1, 0, 0),
    new PowerPlant(0, 1, 0)
};

Would that help you? Let me know if it does.

i think it is better to create PowerPlant class with 3 properties Dam, Diversion and First.

public class PowerPlant
{
    public int Dam { get; set; }
    public int Diversion { get; set; }
    public int First { get; set; }
}

And create a list of the objects

static List<PowerPlant> powerPlants = new List<PowerPlant>
{
    new PowerPlant { Dam = 1, Diversion = 0, First = 1},
    new PowerPlant { Dam = 1, Diversion = 0, First = 0},
    new PowerPlant { Dam = 0, Diversion = 1, First = 0}
};

Then you can use linq to select the data as you want.

What do you want to do with the data?

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