+ 4

Taking multiple values from the user

what will happen if i write the code int age=Convert.ToInt32(Console.ReadLine()); int sal=Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Your age is {0}",age); Console.WriteLine("Your salary is {1}",sal); What error will come and why? Please do help. Thank You

28th Aug 2018, 2:49 PM
Kushagra Mishra
Kushagra Mishra - avatar
5 Answers
+ 5
Your mistake is the placeholder in the second writeline it should be 0 instead of 1. See here https://code.sololearn.com/cpi7Sd2SEKx4/?ref=app
28th Aug 2018, 5:29 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 4
Sorry, I meant placeholder.
28th Aug 2018, 6:22 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 4
Starting with C# 6 we already have another way. Formally called string interpolation. You can write code like this. Console.WriteLine(
quot;Your age is {age}"); this new approach allows you to directly embed the variables themselves. and you clearly see which variables value you will have in output. By the way every Console.WriteLine begins from 0, so that why in second Console.WriteLine you must write {0} , instead of {1}.
30th Aug 2018, 5:27 AM
Nelly Gevorgyan
Nelly Gevorgyan - avatar
+ 2
playground or visual studio
28th Aug 2018, 3:06 PM
sneeze
sneeze - avatar
+ 2
@DZZZ. Well done did not spot that one. Please may I point out a Console.WriteLine("Your age is {0}",age); {0} is a placeholder int *ip; *ip is a pointer
28th Aug 2018, 6:18 PM
sneeze
sneeze - avatar