+ 8
If the code is : int x=4; int y=2; int z=x%y; Console. WriteLine("value of z is {2}",z);
Its value should be.. Value of z is 0,, then why this code is showing error...??
5 Answers
+ 29
Ok, in the string "value of z is {2}" , you will first need to ask yourself, what is {2} in that statement?
In your Console.WriteLine, it detects 'z' as element number {1} . Why is that so? Because it will make no sense if it has x as element number 0 or y as element number1 since x and y is not inside the input of Console.WriteLine; the system does not detect something as an element unless its inside the input.
But writing "value of z is {1}" will still get you an error. This is due to the fact that the system,WriteLine,starts counting it's element number after the string. Beacuse it treats string as what it needs to write , not what it needs to store. Hence, z is actually element number {0} because the string is 'ignored'.
so the correct one will be:
Console.WriteLine("Value of z is {0}" , z);
(Pretty complicated, I know.)
Alternatively, a simplier way will be
Console.WriteLine("Value of z is " + z);
^^ for that method, remember to add a space after the word 'is', the system will not automatically add a space for you.
+ 1
you have to write {0}
0
Thanq, for explaining the basic concept. :)
0
well explained
0
0