Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Console Input in C#

0.00/5 (No votes)
29 Dec 2003 3  
This article was written to provide a brief overview of console input in .NET

Introduction

The purpose of this example is to provide people with a simple class library that should be sufficient to handle all of your console IO needs. I started off calling it Console IO but now that I'm finished with it I realize there is no real output involved in this class library and its just Input and parsing.

Background

As a computer science major I first started off with java since it's what is used at the university I attend. For most people understanding simple programming concepts are difficult enough so a class is usually provided to new programmers for handling console input. Well the first class that I had to use which was used as a reference for this class was SavitchIn which was provided with our java text book (Because everyone knows Console Input in java is ridiculous). As I Started using c# I realized that even though console IO is very easy its not obvious what to do with this information once you have read it from the console so I felt I'd provide this class for anyone starting off their programming experience with c#.

There are very few concepts that the beginning programmer needs to know in .net to be able to use this class.

  1. You must have an understanding of the simple data types.
  2. While it is not necessary exception handling is useful since some of the methods can possibly pass an exception up the stack.

Since the class is very simple there is no need for a class diagram for it. There is a single namespace called ConIO. Within this namespace resides one class called ConInput which is properly named. This is a class containing all static methods for reading input. All of the methods have proper documentation and work properly with Intellisense. The only thing that is needed is to reference the library in any of your projects and you are good to go.

The solution is setup so that the files for the demo program and for the class are in the same solution. The demo program should be set as the start up project. If it is not just right click on the project for ConInputTest and click set as startup project.

Using the code

using ConIO;
//Now that ConIO is being used we only need to 

//type ConInput and the method name.

string input = ConInput.ReadLine();
UInt16 number = ConInput.ReadLineUInt16();

Points of Interest

I know this topic is very basic but I intended this to be part of a series of sample programs to showcase beginning programming concepts in c#. I would greatly appreciate any feedback on this class no matter how simple it may seem.

History

  • Version 1.0 Initial Release

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here