//This is my code,how do i find & print max value
namespace StartCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
           int [, ] a =  new int [4,4] {{2,3,4,5}, {34,56,25,67}, {22,44,55,77},{45,80,22,13}};

           foreach (int x in a)
               Console.WriteLine(x);



        }    
    }
}
///Thank you.

Recommended Answers

All 3 Replies

You can try this

 int[,] a = new int[4, 4] { { 2, 3, 4, 5 }, { 34, 56, 25, 67 }, { 22, 44, 55, 77 }, { 45, 80, 22, 13 } };
 Console.WriteLine(a.Cast<int>().Max());
//Thank you. Can you explain why if we use standard array,like this 
  int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

        int maxNum = numbers.Max();

        Console.WriteLine("The maximum number is {0}.", maxNum);


       // No need to use cast,but in case multidimensional array need to cast.

Beacause the Max() extension will not work on multidimentional array, so first you need to flatten it.

Then the Cast<T>() can do it for you.

Look at this.

foreach (var item in a.Cast<int>())
{
    Console.WriteLine(item);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.