How to print 2D array to console in C#

后端 未结 10 1612
野性不改
野性不改 2020-12-06 22:59

I dont\'t have any code for this, but I do want to know how I could do this. I use visual studio 2010 C# if that matters.

Thanks

Jason

10条回答
  •  醉话见心
    2020-12-06 23:16

    The following is an example...

    static void Main()
    {
        // A. 2D array of strings.
        string[,] a = new string[,]
        {
            {"ant", "aunt"},
            {"Sam", "Samantha"},
            {"clozapine", "quetiapine"},
            {"flomax", "volmax"},
            {"toradol", "tramadol"}
        };
    
        // B. Get the upper bound to loop.
        for (int i = 0; i <= a.GetUpperBound(0); i++)
        {
            string s1 = a[i, 0]; // ant, Sam, clozapine...
            string s2 = a[i, 1]; // aunt, Samantha, quetiapine...
            Console.WriteLine("{0}, {1}", s1, s2);
        }
    
        Console.WriteLine();
    }
    

提交回复
热议问题