Reputation: 910
I'm trying to define a custom type for my project. I will try to explain what I'm trying to achieve. I have n*m packets. each packet has a different composition (mass of CO2, H2O, NO, CO , ...). I want to store the composition of all packets in my custom type. The composition of each packet can be a Dictionary like this:
Dictionary<string, double>
as an example :
packetComposition["CO2"] = 2.6;
packetComposition["O2"] = 1.7;
The point is that all these dictionaries should be stored in some sort of nm array. So I can have composition of all nm packets in one place.
Obviously this array can't be something like double[,] or int[,] , ... because the content will be a dictionary and not an int
or double
.
Any idea how to store all these dictionaries in a custom type of n*m dimension? Thanks
Upvotes: 0
Views: 535
Reputation: 354
You could create your own packet type which will contain all the compounds, for example
public class Packet
{
public Dictionary<string, decimal> Compounds { get; }
public Packet()
{
Compounds = new Dictionary<string, decimal>();
}
}
And you can use it like this
List<Packet> packets = new List<Packet>();
Packet packet = new Packet();
packet.Compounds.Add("CO2", 2.6);
packet.Compounds.Add("O2", 1.7);
packets.Add(packet);
Upvotes: 1
Reputation: 117175
You can certainly use a Dictionary<string, double>[,]
to hold your data:
var packets = new Dictionary<string, double>[2, 3]
{
{ new Dictionary<string, double>(), new Dictionary<string, double>(), new Dictionary<string, double>(), },
{ new Dictionary<string, double>(), new Dictionary<string, double>(), new Dictionary<string, double>(), },
};
packets[1, 0]["CO2"] = 2.6;
packets[1, 1]["CO2"] = 2.4;
packets[1, 2]["CO2"] = 2.9;
Upvotes: 2
Reputation: 37123
You should create your own class for this. Then you can easily create instances and put them into a list:
class Composition
{
public string Name { get; set; }
public double Value { get; set; }
}
Now put them into a multidemensional array:
Composition[,] myInstances = new Composition[n,m];
myInstanes[0,0] = new Composition { Name = "CO2", Value = 2.6 };
myInstanes[0,1] = new Composition { Name = "O2", Value = 1.7 };
Upvotes: 0