Timothy Goodes
Timothy Goodes

Reputation: 67

Writing max array value to console window

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.

Upvotes: 0

Views: 175

Answers (3)

B.K.
B.K.

Reputation: 10162

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()}")

Upvotes: 4

JOSEFtw
JOSEFtw

Reputation: 10091

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

Upvotes: 6

Hamid Pourjam
Hamid Pourjam

Reputation: 20764

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

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

Upvotes: 1

Related Questions