Latest News
Mar 27, 2017, 7:16 AM
using System;

// This tutorial will show examples of using basic input/output and manipulation
// methods of the console class.

namespace ConsoleTutorials
class BasicsTutorial
static void Main(string[] args)
// Console.WriteLine writes an entire line to screen then moves to the next line
Console.WriteLine("This is Tutorial1 - Basics");

// Console.Write writes the text to screen, but doesn't move to the next line.

// Using Console.Write, there are two ways to go to the next line.
// This first is the best recommended as it automatically detects the proper new line sequence.
// This is the old standard often used on Linux to represent a windows new line
// \r represents a carriage return
// \n represents a new line

// Sets the title of the console window to whatever you want.
Console.Title = "Console.Title";

// Sets the background color
Console.BackgroundColor = ConsoleColor.DarkBlue;
// Sets the foreground color
Console.ForegroundColor = ConsoleColor.Yellow;

// Set if the cursor is visible in the console window or not
Console.CursorVisible = true;

// Set this to true to prevent CTRL-C from being treated as an interrupt.
// Default for this is false
Console.TreatControlCAsInput = true;

// Detect if the user has thier caps-lock on
if (Console.CapsLock == true)
Console.WriteLine("Your caps lock is turned on.");
Console.WriteLine("Your caps lock is turned off.");

// Detect if the user has thier num-lock on
if (Console.NumberLock == true)
Console.WriteLine("Your num lock is turned on.");
Console.WriteLine("Your num lock is turned off.");

// These will cause errors unless they are the very first things in
// the main class
// Set the distance the console window is from the top of the screen in pixels
//Console.WindowTop = 0;
// Set the distance the console window is from the left of the screen in pixels
//Console.WindowLeft = 0;
// Set how tall the window is in pixels
//Console.WindowHeight = 600;
// Set how wide the window is in pixels
//Console.WindowWidth = 800;

// Set how far from the left the cursor is in characters
Console.CursorLeft = 0;
// Set how far from the top the cursor is in characters
Console.CursorTop = 15;

Console.WriteLine("Please type a line of characters then press ENTER");
// Read a line of characters from the standard input stream(keyboard)
// This means it won't stop until you press enter
string Entry = Console.ReadLine();

Console.WriteLine("Please press ESCAPE");
// Read a single key press from the keyboard
// This is usefull for detecting function and arrow keys.
ConsoleKeyInfo key = Console.ReadKey();
if(key.Key == ConsoleKey.Escape)
Console.WriteLine("You pressed escape.");

Console.WriteLine("Please press a letter or number.");
// Read the next character pressed on the keyboard
// Returns the value as an integet represtantion of an ascii value
int ikey = Console.Read();
Console.WriteLine(string.Format("You pressed {0}", Convert.ToChar(ikey)));

Console.WriteLine("Press any key to continue . . .");
int tmp = Console.Read();
Mar 27, 2017, 7:06 AM

I’ve written a very helpfull ErrorLog Event class. When used correctly, it raises an event that you can use to process errors however you please.

The main benefit to using this class is if you include it in a Code Library project, it provides a means of returning error messages to the application so the application can handle it.

It does not however return the Exception itself, but you can modify it easily to do that if you need to. What it does return is a string containing all error messages and stacktrace’s for the error, including any and all inner exceptions.

This tutorial is also a very good, and basic example of using Delegates to setup event handling between a DLL and an application. (Remember, DLLs normally only communicate with applications via return values from functions. This allows extra communications outside of return values.)

This tutorial is no longer offered as a viewable Compilr project, but the source code is available below. The code has been modified to use function overloads so that strings and exceptions are passed using the same method name.

NOTE: This code will not do anything by itself. It must be included in a project.

NOTE: While this code does work to perform some simple error logging. I would highly advise using something like log4net instead.

DEBUGGING NOTE: Normally we want error handlers in every function, however since this is the error handler, if we have errors here we really have some serious issues, but will never know about them as they won’t be reported back to the application anyways. For this reason, we’ll trap the errors here, but not handle them. This way at least the application won’t crash.

USAGE: To use this, simply include it in your project, then write your catch blocks like this:

catch(Exception ex)

Then in your application, in it’s main method, before anything else, put this:

ErrorLog.Added += newErrorLogAdded(ErrorLog_Added);

Then simply add the following function, and modify it to handle the error messages as you please.

void ErrorLogAdded(stringMessage)

Here’s the class:

using System;

namespace Utilities
// We need a delegate for the event, to be used in the application for
// detecting when the event was fired, and declaring what method to handle
// it with
public delegate void ErrorLogAdded(stringMessage);
public static class ErrorLog
// Create the event
public static event ErrorLogAdded Added;

// Method to add a string to the error handler, and fire the event
public static void Add(stringResponse)
// There's no processing to be done here, so just fire the event off
catch { }
// Method to add an exception to the error handler, and fire the event
public static void Add(Exception ex)
// This is usefull for avoiding error messages in a threaded environment
// where the thread is shutting down.
if(ex.Message != "Thread was being aborted.")
// Start the message with the top error message and stacktrace
string Message = string.Format("{0}\r\n{1}", ex.Message, ex.StackTrace);
// Create a local copy of the exception
Exception ce = ex;
// Add the inner exception message and stacktrace to the string
Message = string.Format("{0}\r\n{1}\r\n{2}", Message, ce.InnerException.Message, ce.StackTrace);
// Move to the inner exception for the next iteration
ce = ce.InnerException;
// We've built the message string, fire the event off.
catch { }