Difference
between Read(),Readline() and ReadKey() in C#
-
Read() Readline() ReadKey() Accept the string value and return the string value. Accept the string and return IntegerAccept the character and return Character
Declaration
in Application for Read: Console.Read();
Declaration
in Application for Readline(): Console.Readline();
Declaration
in Application for ReadKey(): Console.ReadKey();
Summary:
1.The
above mentioned three methods are mainly used in Console application
and these are used for return the different values .
2.If
we use Read line or Read() we need press Enter button to come back to
code.
3.If
we using Read key() we can press any key to come back code in
application
I am not satisfied with the post
ReplyDeleteConsole.Readline() accepts the string and returns the string.
Console.Read() accepts the first character of the string and returns ASCII Code(Integer Value) of that first character
Console.ReadKey accepts the Character entered in the command prompt and returns the key info pressed with the modifiers(cntrl,shift,Alt) used while pressing the key.Info like which key pressed,ASCII Code of the Key and Modifiers used and the type of the key which will always be char.
Apart from this Console.ReadLine() and Console.Read() will return to the code only when we press Enter Key, whereas in Console.ReadKey() we will return to the Code when we press any key with modifiers or without modifiers.
ThankingYou
Deepak Khantwal
100% true, i agree with u buddy.
DeleteDeepak you are write thanks for commenting that help me too
DeleteDeepak your comment is true.
DeleteGeting really confused...HUH!! u people are confusing meh!! anyway i got the answer..
ReplyDeletecan anyone give code example for all three, which would great for me to clearly understand the concept.
ReplyDeleteFor Readline:
Delete/** Console.Readline reads the current Line as a string and returns whole string */
Console.Write("Please Enter Something : ");
string str1 = Console.ReadLine();
Console.WriteLine(str1);
Console.ReadLine();
Output will be
Please Enter Something : Deepak /**Press Enter
It will print : Deepak
For Read
/*Consol.Read() reads the First Character of the string or a line and returns ASCII Code*/
/* returns to the code window only after pressing the Enter Key*/
Console.WriteLine("Please Enter Something : ");
int str = Console.Read();
Console.WriteLine(str);
Console.ReadLine();
Output will be
Please Enter Something : Deepak /**Press Enter
It will print : 100
For ReadKey
/* ConsoleReadKey() reads the first character that have entered in the command and returns the keys information like
* key name , ASCII Code, Which Modifiers(Cntrl,Shift,Alt) are Pressed with the key*/
/* returns to the code window only after pressing any key with modifiers or without modifiers*/
System.ConsoleKeyInfo obj = new ConsoleKeyInfo();
obj = Console.ReadKey();
Console.WriteLine();
Console.WriteLine("Key : {0},KeyChar is :{1},Modifier is :{2}", obj.Key, obj.KeyChar.GetTypeCode(), obj.Modifiers.ToString());
Console.ReadLine();
Output will be
Please Enter Something : D /**Press Enter
It will print : Key : D , KeyChar is Char, Modifier is 0
//Hope you will Understand now
Thanx
For Readline:
ReplyDelete/** Console.Readline reads the current Line as a string and returns whole string */
Console.Write("Please Enter Something : ");
string str1 = Console.ReadLine();
Console.WriteLine(str1);
Console.ReadLine();
Output will be
Please Enter Something : Deepak /**Press Enter
It will print : Deepak
For Read
/*Consol.Read() reads the First Character of the string or a line and returns ASCII Code*/
/* returns to the code window only after pressing the Enter Key*/
Console.WriteLine("Please Enter Something : ");
int str = Console.Read();
Console.WriteLine(str);
// Console.ReadLine();
Output will be
Please Enter Something : Deepak /**Press Enter
It will print : 100
For ReadKey
/* ConsoleReadKey() reads the first character that have entered in the command and returns the keys information like
* key name , ASCII Code, Which Modifiers(Cntrl,Shift,Alt) are Pressed with the key*/
/* returns to the code window only after pressing any key with modifiers or without modifiers*/
System.ConsoleKeyInfo obj = new ConsoleKeyInfo();
obj = Console.ReadKey();
Console.WriteLine();
Console.WriteLine("Key : {0},KeyChar is :{1},Modifier is :{2}", obj.Key, obj.KeyChar.GetTypeCode(), obj.Modifiers.ToString());
Console.ReadLine();
Output will be
Please Enter Something : D /**Press Enter
It will print : Key : D , KeyChar is Char, Modifier is 0
//Hope you will Understand now
Thanx
this answer is most justible ......thanks deepak.
DeleteVery well explained , for a beginer like me this means that i invested less time to understand the concept.Thank you !
DeleteThank u So much Deepak...I like ur post.....Excellent
ReplyDeleteHi All even i tried this concept. plz don't be confuse..
ReplyDeleteanswer given by Deepak Khantwal is 100% true.
Thank You DEEPAK Khantwal...
ReplyDeleteI was so confused in differentiating these things...but now it completely cleared.
More.....C# Interview Question and answers
ReplyDeleteLing
i was confused these things but i cleared
ReplyDeleteThis comment has been removed by the author.
ReplyDeletetq so much its very useful to me
ReplyDeleteI am a regular reader of your blog and being students it is great to read that your responsibilities have not prevented you from continuing your study and other activities. Love
ReplyDeletepython Online training in chennai
python Online training in bangalore
Superb. I really enjoyed very much with this article here. Really it is an amazing article I had ever read. I hope it will help a lot for all. Thank you so much for this amazing posts and please keep update like this excellent article. thank you for sharing such a great blog with us.
ReplyDeleteBest Devops online Training
Online DevOps Certification Course - Gangboard
A good blog always comes-up with new and exciting information and while reading I have feel that this blog is really have all those quality that qualify a blog to be a one.I wanted to leave a little comment to support you and wish you a good continuation. Wishing you the best of luck for all your blogging efforts read this.
ReplyDeleteData Science Tutorial
Data Science training in anna nagar
Data science training in jaya nagar
Data science training in pune
Data Science Training in Marathahalli
Data science training in kalyan nagar