Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

Part 2 - C# Tutorial - Reading and writing to a console

Suggested Videos

In this video, we will discuss
1. Reading from the console
2. Writing to the console
3. Two ways to write to console
     a) Concatenation
     b) Place holder syntax – Most preferred



Code samples used in the demo
using System;
class Program
{
    static void Main()
    {
        // Prompt the user for his name
        Console.WriteLine("Please enter your name");
        // Read the name from console
        string UserName = Console.ReadLine();
        // Concatenate name with hello word and print
        Console.WriteLine("Hello " + UserName);

        //Placeholder syntax to print name with hello word 
        //Console.WriteLine("Hello {0}", UserName);
    }
}

Please note that C# is case sensitive language.



Part 2 - C# Tutorial - Reading and writing to a console

25 comments:

  1. I am not able to view the videos.
    What is the problem how to solve it suggest me.
    Does it require any player to view videos.

    ReplyDelete
  2. Just goto youtube via
    http://www.youtube.com/user/kudvenkat/videos?view=1&flow=grid
    you ll get all the videos and watch

    ReplyDelete
    Replies
    1. Hi, thank you very much for taking time to answer and help others. Keep it up. Good Luck.

      Delete
    2. Its my pleasure sir. You are doing a lot for us ... you are one of my favourite.

      Delete
  3. Hello Venkat :),

    You're doing an amazing job with these videos. Keep up the good work. It's really appreciated.

    How come I can call the ReadLine method without storing its returned value in a string variable? What's happening to the returned value in this case?

    Thank you :).

    ReplyDelete
    Replies
    1. If you want the program to wait, without closing automatically when it has finished execution, we usually use Console.ReadLine(). Hope this answers your question.

      Delete
  4. hello sir, this video has been cleared my concept about placeholder.
    but only one doubt is why u not taken Console.ReadLine();
    after Hello statement...

    ReplyDelete
    Replies
    1. hi vinay i think that can be done....i just started reading C#...burt i can say it will work in that way also.

      Delete
    2. Console is that black environment.

      To write to it we use Console.WriteLine and to read(input) we use Console.ReadLine().

      Since, we are writing to console, so we will use Console.Write

      Delete
  5. Hi,
    Do you have tutorial for windows programming ?. If so please upload them

    ReplyDelete
  6. Really nice work. Excellent job. I am new to this topic and this is very helpful to get started.

    ReplyDelete
  7. Respected Venkat sir,

    I am great fan of you, you are doing great Job. Praying for your best future and prosperity.

    Thanks for such a great efforts.

    ReplyDelete
  8. Hi Venkat,

    I am not getting the Desired output , instead its takind the parameters and closing off.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApplication2
    {
    class Program
    {
    static void Main(string[] args)
    {


    System.Console.WriteLine("Please enter you First name");

    string Firstname = System.Console.ReadLine();

    System.Console.WriteLine("Please enter you Last name");

    string Lastname = System.Console.ReadLine();

    System.Console.WriteLine("Hello {0} , {1}", Firstname, Lastname);

    }
    }
    }

    ReplyDelete
    Replies
    1. To see your result you can either run your program by running the application by hitting (ctrl+F5 ) or you can add System.Console.ReadLine(); like below...
      System.Console.WriteLine("Hello {0} , {1}", Firstname, Lastname); System.Console.ReadLine();

      Delete
    2. System.Console.WriteLine("Hello {0} {1}", Firstname, Lastname);
      System.Console.ReadLine();

      Delete
  9. Dear Sir first of all thanks for these nice and much easy to understand videos.
    Sir i want to get date of birth of my client and calculate it in days and months and years i cant do this can u please guid me using code?

    ReplyDelete
  10. Thanks a lot really great Great teaching, I have experienced in my Career. Can you please provide documents for C# videos. It will be great help for us.

    ReplyDelete
    Replies
    1. You can find the slides that is used in this course at
      http://csharp-video-tutorials.blogspot.se/p/c.html

      Delete
  11. Hello Sir, First of all thank you. I have a question, I have copied the exact code and it works fine but unlike your program it closes right after displaying the output... in C++ we had _getch() or system("pause") to prevent this.. what can be used here?

    ReplyDelete
    Replies
    1. You have to add following in the end of your code (in main):
      Console.ReadLine();
      to make the program to stop
      (or instead add in the end of yor code
      Console.ReadKey();

      With Console.ReadLine(); the program is waiting for an input from the user´s keyboard (followed by an Enter ) (This input is not used in this case).
      OR
      with Console.ReadKey(); the program is wating for ONE key to be pressed at the users´s keyboard (The code for that keyboardkey is not used in this case)
      Both alternatives makes the program to stop & wait for input and
      meanwhile you can see the printout.

      Delete
  12. hi venkat,
    Please tell what is the difference between
    Console.WriteLine("Hello " + UserName);
    and
    Console.WriteLine("Hello {0}", UserName);

    here what is the use of place holder

    ReplyDelete
    Replies
    1. console.writeline("Hello" + username) => it is called concinnate method used
      Console.WriteLine("Hello {0}", UserName) Here placeholder is used

      Delete
  13. hai venkat,
    my cmd window is not displayed yet.it will suddenly disapper to run the program.which key word is used

    ReplyDelete
  14. Hey venkat,

    You Are doing a Awesome job here for The Ppl like us, From beginner to Pro everyone watches your Tutorials.
    I really appreciate Your Work.

    Peace:)

    ReplyDelete
  15. So... in my previous statement I need to correct and myself and tell the following. That ...
    the command: string UserName = Console.ReadLine();
    this ONLY command executes from the back to the front because of the assign (=) operator. So, this command is actualy two commands because of the assign operator.
    And to be more specific we can tell that:
    When we have int i = 10; we usualy say that i equals to 10 but the correct expression is that "the constant 10 assigned to variable i".
    This thinking explains the statement that the command with the string variable and the assigned operator executes from back to front. And the Console.ReadLine() function executes before the creation of the variable UserName.

    Thanks.

    ReplyDelete

It would be great if you can help share these free resources