Sunday 15 June 2014

c# - The simplest way to have a strongly typed array with 2 datatypes? -


I do not want anything sorting or fancy. I just want 2 columns that with the left name and the numbers along the right side

Something like this:

  string / int [,] myArray = new string / int [,] ();   

Each string will be related int but I sort it or do not want anything. I know that I can use a dictionary and other advanced methods that I know how to use it. I want to get simple, I want to see it in simple form how can I create an array with 2 types in this way. In the simplest way I can think of the use of the object, and then clearly convert later. An easy way to do this?

The easiest way to do this is to code to create class tuples You can use TUPAL with the generic parameter to combine. For example, Tupal & lt; String, intactive or toplet & lt; String, int, int ?, char & gt; 2-parameter version is documentation. In the case you've described, you might find the Tuple & lt; String, int & gt; [] .

For an example of type, you can access the elements of Tuple using the ItemX methods: myTuple.Item1 , myTuple.Item2 , etc.

To really create tuples, I recommend using the Tuple.Create methods. For example: Tuple.Create ("Hello", 1) with a set of items set correctly a Tuple & lt; String, int & gt; Will return these methods allow you to leave the normal parameters.

As a side note, you have mentioned that you do not need sorting. You can see the new Tupl and lieutenant; Ease of arbitrary ordering by executing a LINQ query on string, at AG (which is IEnumerable & lt; Tuple & lt; string, int> gt; & gt; , and applies) You can get from So you can do the IEnumerable extension of LINQ on it). For example, to sort by int part and then the string part of the tuple, you can:

  myTupleArray.OrderBy (t = & gt; T.Item2). Since then (T = & gt; T. Item2);   

Also, see order-endandending and transsexuals for more options.

No comments:

Post a Comment