Let us Learn Tuple-Part3 ( Tuple and Arrays)

Rajnilari2015
Posted by in C# category on for Beginner level | Points: 250 | Views : 3250 red flag

In this third part, we will learn How Tuples interact with Arrays.


 Download source code for Let us Learn Tuple-Part3 ( Tuple and Arrays)

Introduction

This is the third part of the series Let us Learn Tuple. In this third part, we will learn How Tuples interact with Arrays.

You can read the entire series of article as listed down

Let us Learn Tuple-Part1

Let us Learn Tuple-Part2 ( Creation of Generic Collections and Accessing the same using Tuple )

Let us Learn Tuple-Part3 ( Tuple and Arrays )

Let us Learn Tuple-Part4 ( Create MultiKey dictionary using Tuple )

Let us Learn Tuple-Part5 ( LAMBDA with Tuple )

Let us Learn Tuple-Part6 (Tuple Serialization and DeSerialization)

Let us Learn Tuple-Part7 (Last but not the least part)

Using the code

Creating an array of Tuple is different from creating any other object with Tuple. In this case, we should not specify an argument list for the constructor as we have seen so far in the earlier examples.

We can create a 1-D rectangular array of Tuple as under

//Creating an array of capacity 10
Tuple<int>[] int1DArray = new Tuple<int>[10]; 

//Pushing data to the 1D Rectangular Array
for (int i = 10; i--> 0;){
    int1DArray[i] = Tuple.Create(10 * i);
}

//Accessing data from the 1D Rectangular Array
for (int i = 10; 0 <-- i){
    Console.WriteLine($"Element at {i}  is {int1DArray[i].Item1} ");
}

First we have created a 1D array of Tuple whose capacity is 10. We can figure out that we are not using the () for instantiating it. Once the Tuple is created, we have pushed some values to the Tuple-Array by using the "goes to" operator. Finally we are reading the data from the Tuple-Array by using the "is approached by" operator.

N.B.~ Please don't be under the impression that, "goes to" and "is approached by" are literally single operator. They are two different operators -- and > or <. The -- is for decrementing the value. The > or < is for comparison with the right-hand or left-hand operand. A more readable version will be (i--) > 0

In a similar way we can create a 2-D rectangular array of Tuple as under

Tuple<int, int>[,] int2DArray = new Tuple<int, int>[2, 3];//creating an array of capacity 2,3

//Pushing data to the 2D Rectangular Array
for (int row = 0; row < 2; row++)
{
    for (int col = 0; col < 3; col++)
    {
        int2DArray[row,col] = Tuple.Create((row+1),(col+1));                    
    }               
}

//Accessing data from the 2D Rectangular Array
for (int row = 0; row < 2; row++)
{
    for (int col = 0; col < 3; col++)
    {
        Console.WriteLine($"Element at row {row} is  {int2DArray[row, col].Item1} and col {col} is {int2DArray[row, col].Item2} ");
    }
}

In a similar way we can create any number of N-D Rectangular Array of Tuples

Jagged Array of Tuples

An array whose elements are also arrays is a jagged array.We will look into how to create and access a jagged array with Tuples

//create a jagged array
int[][] sampleJaggedArray = new int[][]
    {
        new int[] {10,20,30,40,50,60},
        new int[] {1,2,3,4,5,6,7,8,9,10,11,12},
        new int[] {11,22,33,44},
        new int[] {-1,-2,-3,-4,-5,-6}
    };            

//create an tuple of jagged array
var jaggedArrayTuple =   Tuple.Create(sampleJaggedArray);

//access the tuple of jagged array created 
var elementOf4thRow2ndColumn = jaggedArrayTuple.Item1[3][1]; //element of 4th Row and 2nd Column [ -2 ]

Jagged Array and N-Dimensional Array of Tuples

Jagged arrays can be combined with multidimensional arrays. The below is an example that shows how to create and access a 1D jagged array which contains four 2D array elements with Tuples

 //Example of Jagged Array and 2D Rectangular Array of Tuples
int[][,] sampleJaggedArrayWith2DRectangularArray = new int[4][,]
{
    new int[,] { {1,2}, {3,4} },
    new int[,] { {5,6}, {7,8}, {9,10} , {11,12}},
    new int[,] { {14,15}, {16,17}, {18,19}, { 20, 21 } },
    new int[,] { {22,23}, {24,25} , { 26, 27 }, { 28, 30 } }
};

//create an tuple of Jagged Array and 2D Rectangular Array
var jaggedArrayWith2DRectangularArrayTuple = Tuple.Create(sampleJaggedArrayWith2DRectangularArray);

//access the tuple of Jagged Array and 2D Rectangular Array created
 
//element of 2nd row 3rd column 2nd Element [ 10 ]
var elementOf2ndRow3rdColumn = jaggedArrayWith2DRectangularArrayTuple.Item1[1][2,1]; 

Conclusion

In this article we learnt about How Tuples interact with Arrays. Hope this will be helpful. More to come in the following series. Zipped file attached.

Page copy protected against web site content infringement by Copyscape

About the Author

Rajnilari2015
Full Name: Niladri Biswas (RNA Team)
Member Level: Platinum
Member Status: Member,Microsoft_MVP,MVP
Member Since: 3/17/2015 2:41:06 AM
Country: India
-- Thanks & Regards, RNA Team


Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)