0

I am sure there is something stupid i am missing. I want to print the message to console window and in the same line show the max array value.

When i run the code without the console message it runs perfect but when i run the code with the message, it only shows the message and no max value.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Arrays
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] newArray = new int[6];

            newArray[0] = 0;
            newArray[1] = 1;
            newArray[2] = 2;
            newArray[3] = 49;
            newArray[4] = 3;
            newArray[5] = 82;

            Console.Write("The highest number in the array is: ",  newArray.Max());
            Console.ReadLine();
        }
    }
}

I am just starting to get the hang of arrays but i cannot find a solution to the above problem.

Hamid Pourjam
  • 20,441
  • 9
  • 58
  • 74
Timothy Goodes
  • 67
  • 3
  • 13

3 Answers3

6

Try this

Console.Write("The highest number in the array is: {0} ", newArray.Max()); 

You can read more about string.format here: Why use String.Format?

And here Getting Started With String.Format

Community
  • 1
  • 1
JOSEFtw
  • 9,781
  • 9
  • 49
  • 67
4

One way is to concatenate the string:

Console.Write("The highest number in the array is: " + newArray.Max());

Another way is through a composite format string and a parameter:

Console.Write("The highest number in the array is: {0} ", newArray.Max()); 

Lastly, if you have Visual Studio 2015, you can do string interpolation:

Console.WriteLine($"The highest number in the array is:{newArray.Max()}")
B.K.
  • 9,982
  • 10
  • 73
  • 105
1

You can also use new C# 6.0 feature called string interpolation

Console.Write($"The highest number in the array is: {newArray.Max()}");
Hamid Pourjam
  • 20,441
  • 9
  • 58
  • 74