.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal

Home >> Forum >> ASP.Net >> Post New QuestionBookmark and Share Subscribe to Forum

Code Logic doubt for 2 D Array

Posted By: pooja     Posted Date: March 31, 2010    Points:2   Category :ASP.Net
Hi frnds...

i have the follwing for 2 D array with for loop

public class MultidimensionalArrays
public static void Main()
int[,] matrix = { { 1, 1 }, { 2, 2 }, { 3, 3 } };

for (int i = 0; i < matrix.GetLength(0); i++)
for (int j = 0; j < matrix.GetLength(1); j++)
Console.WriteLine("matrix[{0}, {1}] = {2}", i, j, matrix[i, j]);


in the above code i want to know the following lines ...



"matrix[{0}, {1}] = {2}", i, j, matrix[i, j]);

please explain while code executing what the above lines does exactly as per the logic....

thanx in advance

Author: Dilip             
Posted Date: March 31, 2010     Points: 5   

These are basics of C# programming

First Question

int[,] matrix = { { 1, 1 }, { 2, 2 }, { 3, 3 } };

If you see properly, the above is array of arrays i.e. 2 dimensional. Each element of first array is holding another array of any specified length.

matrix.GetLength(0) gives the size of parent array
matrix.GetLength(1) gives the size of child array in each element.

2nd Question

"matrix[{0}, {1}] = {2}", i, j, matrix[i, j]);

This is string formating. You can format a string output in any fashion using parameters marked as {0},{1},...so on

It is also same as below:

Console.WriteLine("matrix[" + i + "," , j "] = " + matrix[i, j]);

Better way & Easy way is to use string formatting rather than using string concatenation

Let me know if you need any further information


Post Reply

You must Sign In To post reply
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend