The best way to represent sets of 3 values

Suppose I have 100 sets of 3 values. the first value is time, the second value is position x, and the third value is position y. For example, this will be the position of the xy point for more than 100 seconds. I have a php background. in PHP, I would do this with a three-dimensional associative array:

position["time"]["x"]["y"] 

so I can access the x, y values ​​at a specific time. How do you do this in C #? I think generics like list and dictionary will do it. but I don't know how to implement key values ​​for a 3D dataset.

+7
generics c # associative-array multidimensional-array
source share
5 answers

It looks like you want to access the x / y position at a given time. You can consider a combination of other answers along with a dictionary.

Define a simple structure that contains the x and y positions. It is important that this object is immutable.

 public struct Position { public int X { get; } public int Y { get; } public Position(int x, int y) { this.X = x; this.Y = y; } } 

Now you can save them in a dictionary with (possibly) DateTime as a key.

 Dictionary<DateTime, Position> positions = new Dictionary<DateTime, Position>(); positions.Add(new DateTime(2017,3,29,10,0,0), new Position(10,10)); 

And you can read your position, for example

 var pos = positions[new DateTime(2017,3,29,10,0,0)]; Console.WriteLine("{0}:{1}",pos.X,pos.Y); 

A nice addition to this is that if, for example, you get a list of these objects that you want to find by time, you can easily turn them into a dictionary. Say you got a list of them (from another answer):

 public class PositionEntity { public DateTime Time {get;set;} public int X {get;set;} public int Y {get;set;} } 

As an entities list, you can do this:

 IEnumerable<PositionEntity> entities = .... loaded from somewhere var dict = entities.ToDictionary(k => k.Time, v => new Position(vX,vY)); 
+4
source share

In C # there is a concept of types, you will need to create a specific custom type: struct (value type) or class (reference type)

You can create a new type in this case, I create a class that will look like this:

 public class Position { public DateTime Time {get;set;} public int X {get;set;} public int Y {get;set;} } 

Now at some point you will have an instance of the class:

 Position postion = new Position(); // object created positon.Time = DateTime.Now; position.X = 1; position.Y =0; 

and in the same way you will get values ​​from it:

 DateTime time = position.Time; int positionX = position.X; int positionY = position.Y; 

I would suggest reading the basics of type from MSDN here

and for the part part, we have a Framework containing collections in C # that includes Array , a List that can be used to store several Position objects.

Hope this helps!

+7
source share

I suggest defining an immutable type for this purpose and using a dictionary to store values, since an object can be in one place at a specific time.

 public class Position { public Position(double x, double y) { X = x; Y = y; } public double X { get; } public double Y { get; } } 

use it as follows:

 var data = new Dictionary<TimeSpan, Position>(); //add data in two different ways data.Add(TimeSpan.FromMilliseconds(10), new Position(0.1, 1)); data[TimeSpan.FromMilliseconds(15)] = new Position(10, 15); //accessing data var pos = data[TimeSpan.FromMilliseconds(10)]; //check if data exist for specific time if (data.ContainsKey(TimeSpan.FromMilliseconds(15))) { //do what you want } 
+3
source share

A simple solution would be to use Tuple if it suits your needs, for example.

 Example: var tuple = Tuple.Create(someBytes, moreBytes, myString); 

You can access the values ​​using tuple.Item1, tuple.Item2 and tuple.Item3.

You can, of course, also use several tuples if you need it, for example

Example

 var list = new List<Tuple<byte, byte, string>> { tuple, anotherTuple, thirdTuple, etc }; 

As others have pointed out, if you are referring to “similar” data, you should create a class instead.

0
source share

You can use the Tuple class ( See the Tuple class in msdn )

 Tuple.Create("time", "x", "y"); //creates a Tuple with 3 elements, aka triple 
0
source share

All Articles