C# Lesson 1: Reply the Greeting

In this lesson, we would like that

  1. The console window prompt the user to type their name.
  2. The program capture their input as a string/text.
  3. The program reply backs with the greeting.
Here we go!
Code the following (code in copy-able text is available below)

Press F5 to build and run...
Type you name as prompted, press Enter..
Let us now analyze the code
  • We have used Console.ReadLine() to read input from user.
  • We stored user input in a variable of string type. String variable are used to store and manipulate plain text.
  • String concatenation is done by the plus "+" operator as shown in the example.
Here is the code in copy-able text
-----

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

namespace MyApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            // write to the console window
            Console.WriteLine("Hello, May I know you name: ");
            // capture user input as string and store in variable called name
            string name =Console.ReadLine();
            // write to the console window
            Console.WriteLine("Welcome to you "+name+", Have a nice day!");
            // read line from user to avoid auto-window closure
            Console.ReadLine();
        }
    }
}
-----

Comments

Popular posts from this blog

C# Lesson 3: C# While Loop

C# Programming, Lesson 0: Installing Visual Studio