C# I/O Console Demonstrations (.Net)

Facilities of the Console

We quickly converted an introductory example demonstrating facilities of the console from our Oxygene code.

using System;

namespace DotNetConsoleDemoCS
{
  static class Program
  {
    public static void Main(string[] args)
    {
      Console.SetWindowSize(60, 10);
      Console.BackgroundColor = ConsoleColor.Gray;
      Console.Clear();
      Console.ForegroundColor = ConsoleColor.Blue;
      Console.WriteLine("Hello World!");
      Console.Beep(500, 1000); //Note of frequency 500/s for 1 s
      Console.ForegroundColor = ConsoleColor.DarkGreen;
      Console.Write("Please enter your forename: ");
      String forename = Console.ReadLine();
      Console.WriteLine("Hello, " + forename + ". Please press a key combination such as CTRL+J." );
      ConsoleKeyInfo key = Console.ReadKey();
      Console.WriteLine("You pressed " + key.Key + " with " + key.Modifiers.ToString());
      Console.SetCursorPosition(20, 6);
      Console.WriteLine("Finished!");
      Console.Read();
    }
  }
}

Output to the Console

This demonstration shows how to declare an int, Single, Char and String and how to output them to the .Net console. The output of the demo copied from the console follows the code.

using System;

namespace console_output_cs
{
  static class Program
  {
    public static void Main(string[] args)
    {
      int MyInt = 42;
      Single MySingle = 1.2345f;
      Char MyChar = 'a';
      String MyString = "This is my string.";  //Note the double quotes
      // Output one variable at a time.
      Console.WriteLine(MyInt);
      Console.WriteLine(MySingle);
      Console.WriteLine(MyChar);
      Console.WriteLine(MyString);
      // Output a string composed of concatenated substrings.
      Console.WriteLine("Integer value: " + MyInt.ToString());
      // Formatting 
      Console.WriteLine("MyInt: {0}\nMySingle to 2dp: {1:0.00}", MyInt, MySingle); // \n gives new line       
      Console.WriteLine(MyString);
      Console.ReadLine();
    }
  }
}

Output:
42
1.2345
a
This is my string.
Integer value: 42
MyInt: 42
MySingle to 2dp: 1.23
This is my string.

Input to a Console Application

The third example shows how to input a String, Integer and Double to a .Net console program. The output for some acceptable and invalid inputs follows the code.

using System;

namespace console_input_cs
{
  static class InputDemo
  {
    public static void Main(string[] args)
    {
      Console.Write("Please enter a string. ");
      string MyString = Console.ReadLine();
      int InputLength = MyString.Length;
      Console.WriteLine("You entered " + InputLength.ToString() + " characters: " + MyString);

      int intInput;
      Boolean accepted = false;
      String strInput = "";
      do
      {
        accepted = false;
        Console.Write("Please enter an integer: ");
        strInput = Console.ReadLine();
        accepted = int.TryParse(strInput, out intInput);
        if (accepted)         
          Console.WriteLine(strInput + " accepted"); 
        else
          Console.WriteLine("Not an integer!");
      }
      while (!accepted);
      Double doubleInput;
      accepted = false;
      do
      {
        Console.Write("Please enter a real number: ");
        strInput = Console.ReadLine();
        accepted = Double.TryParse(strInput, out doubleInput);
        if (accepted)
          Console.WriteLine(strInput + " accepted"); 
        else
          Console.WriteLine("Not a number!");
      }
      while (!accepted);
      Double Sum = intInput + doubleInput;
      Console.WriteLine("Total: " + Sum.ToString());
      Console.ReadLine();
    }
  }
}

Input and output:
Please enter a string. test
You entered 4 characters: test
Please enter an integer: 42.1
Not an integer!
Please enter an integer: 42
42 accepted
Please enter a real number: 3.1d
Not a number!
Please enter a real number: 3.142
3.142 accepted
Total: 45.142

Programming - a skill for life!

C# Demonstrations of I/O, arrays, methods, classes, loops, enums, pointers, file handling, Firebird databases, LINQ and drawing on a form