Re: Simple math program.
- From: Simon <simon_nospam_rigby@xxxxxxxxxxxx>
- Date: Mon, 05 Dec 2005 16:40:06 +0000
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
.
- References:
- Simple math program.
- From: Eric Anderson
- Simple math program.
- Prev by Date: RE: Solution for storing a lot of web app configuration ?
- Next by Date: Re: Strange Exception in default Hashtable constructor
- Previous by thread: Re: Simple math program.
- Next by thread: Solution for storing a lot of web app configuration ?
- Index(es):
Relevant Pages
|