Write and Read Console

1. Console.WriteLine()

  • This method is used to print a specified text or variable value to the console window.

      Console.WriteLine(value);
    
      // Print a message to the console
      Console.WriteLine("Hello, world!");
    
      // Print the value of a variable
      int age = 25;
      Console.WriteLine("My age is: " + age);
    

2. Console.ReadLine()

  • This method reads a line of text from the standard input stream (usually the keyboard) and returns it as a string.

      string input = Console.ReadLine();
    
      // Read a name from the user
      Console.Write("Enter your name: ");
      string name = Console.ReadLine();
    
      // Greet the user
      Console.WriteLine("Hello, " + name + "!");
    

3. Console.ReadKey()

  • This method reads a single key press from the standard input stream and returns a ConsoleKeyInfo object.

  •   // Wait for a key press
      Console.WriteLine("Press any key to continue...");
      Console.ReadKey();
    

4. Console.Clear()

  • This method clears the console window, removing all text.

      Console.Clear();
    

5. int.Parse()

  • This method converts a string representation of an integer number to its equivalent integer value.

      int number = int.Parse(string);
    
      // Read a number from the user and convert it to an integer
      Console.Write("Enter a number: ");
      string input = Console.ReadLine();
      int number = int.Parse(input);
    
      // Print the number
      Console.WriteLine("You entered: " + number);
    

Note: Assigning to an integer variable:

Since Console.ReadLine() returns a string, we need to convert it to an integer before assigning it to an integer variable. We can use the int.Parse() method for this conversion:

// Prompt the user for input
Console.Write("Enter your age: ");

// Read the input and convert it to an integer
string ageString = Console.ReadLine();
int age = int.Parse(ageString);

// Print the age
Console.WriteLine("Your age is: " + age);

Important Note:

The int.Parse() method can throw an exception if the input string cannot be converted to an integer (e.g., if the user enters a non-numeric value). To handle this, it's recommended to use the int.TryParse() method, which returns a boolean value indicating whether the conversion was successful:

// Prompt the user for input
Console.Write("Enter your age: ");

// Read the input
string ageString = Console.ReadLine();

// Try to parse the input
int age;
if (int.TryParse(ageString, out age))
{
    // Conversion successful
    Console.WriteLine("Your age is: " + age);
}
else
{
    // Conversion failed, handle the error
    Console.WriteLine("Invalid input. Please enter a valid number.");
}

By using int.TryParse(), you can gracefully handle invalid input and avoid potential exceptions.


            Console.Clear(); // Clear the console
            Console.Write("Enter your name: "); // Prompt the user for their name and age
            string name = Console.ReadLine();

            Console.Write("Enter your age: ");
            string ageString = Console.ReadLine(); //Parse the age string to an integer
            int age;
            if (int.TryParse(ageString, out age))
            {
                int birthYear = DateTime.Now.Year - age; // Calculate the year of birth

                // Display the information
                Console.WriteLine("\nHello, " + name + "!");
                Console.WriteLine("You were born in " + birthYear + ".");

                Console.WriteLine("\nPress any key to exit..."); 
                Console.ReadKey(); //Wait for a key press, then exists
            }
            else
            {   // Handle invalid input
                Console.WriteLine("Invalid age input. Please enter a valid number.");
            }