Re: Simple math program.



The {0} is substituted for the appropriate argument (in this case 5+4). The argument evaluates to 9 and is inserted into the string as position {0}. Why did you expect it to be 1? Not having a go, just interested.

Simon

As a further example

Console.WriteLine("5 + 4 = {0} and 5 - 4 = {1}", 5+4, 5-4);

would result in

5 + 4 = 9 and 5 - 4 = 1

Eric Anderson wrote:
Would someone be kind and explain why and how this works. It comes up with the correct answer, but I dont understand how it does. Wouldn't Console.WriteLine("5+4={0}",5+4); wouldn't it come up as 5+4=1 instead of 5+4=9? Sorry guys im new to all of this but im learning. Thanks for the help in advance!

using System;

namespace SimpleMath

{

class DoMath

{

static void Main(string[] args)

{

int a = 1;

//addition with integers works as expected

Console.WriteLine("5 + 4 = {0}", 5 + 4);

Console.Write("Please press \"enter\" to continue");

Console.ReadLine();

} // end main

} // end class

} // end namespace


-- (\(\ (=':') (,(")(") Eric Anderson
.



Relevant Pages