.net - why getting null value from console in c# for readLine() after using read() -
i have following code
char c1 = (char)console.read(); console.writeline("enter string."); string instr = console.readline();
it takes value c1
, after prints "enter string". when try enter string, appears working readkey()
, meaning press key it's showing instr
has null value.
if remove first line (char c1 = (char)console.read();
), program works correctly.
why this?
when call read()
, still blocks until hit enter though actual method consume single character input stream. when subsequently hit enter, character indeed read, newline isn't. since newline still in input stream, call readline()
returns, it's read line terminator. can see behaviour in more depth if debug.
to resolve suggest following, using readkey()
:
char c1 = console.readkey().keychar; console.writeline(environment.newline /* added readability */ + "enter string."); string instr = console.readline();
if user still hit enter after read()
, use readline
, take substring first character.
Comments
Post a Comment