Assume that I have 100 sets of 3 values. first value is time, second value is x position and 3rd value is y position. this is going to be the xy position of a point over 100 seconds for example. I have a PHP background. in PHP I would do it with a 3D associative array like this :
position["time"]["x"]["y"]
so I can access x,y values at a certain time. How would you do this in c#? I think generics like list and dictionary would do that. but I don't know how to implement key values for a 3D set of data.
It looks like you want to access an x/y position at a given time. You might consider a combination of the other answers along with a dictionary.
Define a simple struct which holds x & y positions. It is important that this object is immutable.
Now you can store these in a dictionary with (perhaps)
DateTime
as the key.And you can read your position like
A neat little addition to this is that if, for example, you are getting a list of these object you wish to lookup by time you can easily turn them into a dictionary. Say your get a list of these (from other answer):
As a list
entities
you can do this:I suggest defining an immutable type for this purpose and use a dictionary to store values since an object can be in one place at a specific time.
use it like this:
A simple solution might be to use a Tuple, if this is suitable for your needs, e.g.
You can access the values using tuple.Item1, tuple.Item2, and tuple.Item3.
You can of course also use multiple Tuples if you require this, e.g.
Example
As others have mentioned though, if you're referencing "like" data, you should create a class instead.
In C# there is types concept, you would need to create a concrete custom type either struct (value type )or class (reference type)
You can create a new type in this case i am creating class which would be like:
Now at some point you would have a instance of class position:
and the same way you would get the values back from it :
I would suggest to read basics of type from MSDN here
and for sets part, we have Framework provided collections in C# which includes Array,List which can be used to hold multiple objects of
Position
.Hope It helps!
You can use the
Tuple
class (See Tuple class on msdn)