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

18 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
    Replies
    1. 100% true, i agree with u buddy.

      Delete
    2. Deepak you are write thanks for commenting that help me too

      Delete
  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