Support us .Net Basics C# SQL ASP.NET ADO.NET MVC Slides C# Programs Subscribe Buy DVD

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

19 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
  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

If you like this website, please share with your friends on facebook and Google+ and recommend us on google using the g+1 button on the top right hand corner.