Saturday, March 1, 2014

Represent Tuples via Tuple

In this post I want to show how to represent tuples in C#, similiar to the from C known type pair. In C# this can be done with the class Tuple.
A tuple is initialized as follows:

Tuple<string, int> TestTuple = new Tuple<string, int>("Test", 1);

So for type safety the datatypes to be paired can be specified, on the right side the values of the tuple are then initialized. Here we create a tuple whose first value is a string and second an integer.
Afterwards, these entries are accessible only for read, which can be done via TestTuple.Item1 respectively TestTuple.Item2.

No comments:

Post a Comment