why is convert.toint32 must be there? and not just console.readline (line 4)
Console.WriteLine("whats ur name");
string name = Console.ReadLine();
Console.WriteLine("age?");
int age = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("hello" + name); Console.WriteLine("ure" + age + " yo");
Console.WriteLine("hello" + name); Console.WriteLine("ure" + age + " yo");
36 Replies
what does Console.ReadLine() return?
any line
i just started so if i got right ur question, the first one is when u type ur name and the second is for age
right, but what kind of data does it give you?
to answer and fill information
no?
i'm talking about data types in the language, those affect what you can do with the data
Console.ReadLine() returns a string which is just text
it doesn't know if it's a number or not, so if you want to use the data like a number you have to convert it to a different data type used to represent numbers
in your specific example there's no real point because you just print it back out, but if you wanted to do math with the age or something you would need it
but why do u need to use convert?
if you want to use the data like a number you have to convert it to a different data type used to represent numberstry writing the code without convert and see what happens as an experiment
it works
then you changed more than just removing convert
Console.WriteLine("whats ur name");
string name = Console.ReadLine();
Console.WriteLine("age?");
int age = Console.ReadLine();
Console.WriteLine("hello" + name); Console.WriteLine("ure" + age + " yo"); its the same
Console.WriteLine("hello" + name); Console.WriteLine("ure" + age + " yo"); its the same
that code doesn't compile
why did you say it works?
do you see a red squiggly line on the
int age = Console.ReadLine();
line?yeah yeah it dosent work
because Console.ReadLine gives you a
string
back, not int
if you want an int
you have to convert the string to oneim sorry that it takes me a whil,e i dont speak engllish and my mind is blown up
console read line always gives string?
always
so when i type back
i cant write like hello world
just helloworld
strings can have spaces in them
ReadLine reads a line of text, which means everything you type until you press enter
theres one without space i forgot
ohhhh so when u do invert it makes the readline able to use int
not really
bro what
Moon
console read line always gives string?
Quoted by
<@901546879530172498> from #why is convert.toint32 must be there? and not just console.readline (line 4) (click here)
React with ❌ to remove this embed.
readline never uses int
but you can take the string that readline gives you, and give it to convert.toint32
because that takes in a string and gives you back an int
but it only works if the string actually has a number typed into it
Jimmacle
REPL Result: Failure
Exception: FormatException
Compile: 412.379ms | Execution: 18.820ms | React with ❌ to remove this embed.
bro its so hard
so it dosent make consolereadline able to use int?
correct, you can't change how readline works
but you can take what readline gives you and change it in your code to be something else
that's what convert.toint32 does
wdym by something else
like an int
wdym something else, to change the readline?
again, you can't change what readline does
but you can do things to what readline gives you
because different data types mean different things, strings are text and ints are numbers
Jimmacle
REPL Result: Success
Result: string
Compile: 147.528ms | Execution: 15.932ms | React with ❌ to remove this embed.
Jimmacle
REPL Result: Success
Result: int
Compile: 149.307ms | Execution: 14.743ms | React with ❌ to remove this embed.
u made the string here a number?
Convert is a class in this case which ahas functions, ToInt32 is a function, which takes a string as input. Both string and int are types.
string text = Console.ReadLine();
string is a type
text is the variable
Console is the class
and ReadLine is the function.
The return type of the function specifies what it gives back. void just simply means nothing, but in this case, ReadLine returns a string.
int textAsNumber = Convert.ToInt32(text);
int is a type
textAsNumber is the variable
Convert is the class
and ToInt32 is the function.
The ToInt32 function takes a string parameter, in this case the texxt variable which is a string.it's the same thing you're doing with age, just split into 2 lines
at the risk of beating a dead horse,
the code there says, in english:
1. Read one line of text from the console and store it in the box labeled "text
" 2. Take the value in the box labeled "text
", convert it into anint
, and then store thatint
value in the box called "textAsNumber
"
i understood it, thanks yall very much