8.24.2012

Difference between Read(),Readline() and ReadKey in C#

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 Integer
Accept 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

20 comments:

  1. I am not satisfied with the post

    Console.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

    ReplyDelete
  2. Geting really confused...HUH!! u people are confusing meh!! anyway i got the answer..

    ReplyDelete
  3. can anyone give code example for all three, which would great for me to clearly understand the concept.

    ReplyDelete
    Replies
    1. For Readline:

      /** 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

      Delete
  4. For Readline:

    /** 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

    ReplyDelete
    Replies
    1. this answer is most justible ......thanks deepak.

      Delete
    2. Very well explained , for a beginer like me this means that i invested less time to understand the concept.Thank you !

      Delete
  5. Thank u So much Deepak...I like ur post.....Excellent

    ReplyDelete
  6. Hi All even i tried this concept. plz don't be confuse..
    answer given by Deepak Khantwal is 100% true.

    ReplyDelete
  7. Thank You DEEPAK Khantwal...
    I was so confused in differentiating these things...but now it completely cleared.

    ReplyDelete
  8. i was confused these things but i cleared

    ReplyDelete
  9. This comment has been removed by the author.

    ReplyDelete
  10. tq so much its very useful to me

    ReplyDelete
  11. I 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
    python Online training in chennai
    python Online training in bangalore

    ReplyDelete
  12. 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.
    Best Devops online Training
    Online DevOps Certification Course - Gangboard

    ReplyDelete
  13. 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.

    Data 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

    ReplyDelete