Start course

This course uses file reading and writing as a vehicle to illustrate important data validation concepts when mapping text data to objects and receiving user input. It demonstrates many of the essential elements present in business applications, that is: ingesting data from an external source, enabling users to modify that data, and saving the data back to permanent storage.

Robust and well-designed apps, especially concerning the user interface, need to validate input in terms of data format and applicability and provide feedback when entered data is not appropriate. We round off the course by implementing the data access layer design pattern that will enable the code to be easily adapted to other data sources.

Learning Objectives

  • Read planet data from a text file into a list of planet objects within a solar system object
  • Create a basic command-line user interface with data validation and feedback that writes updated planet data back to file
  • Take the file reading and writing functionality and split it out into a class that returns planet objects so you can easily change data sources in the future

Intended Audience

This course is ideal for anyone who already has some basic knowledge of C# and is looking to expand on that by learning about lists and files within the programming language.


The ideal prerequisite for this course is our C# Loops Deep Dive course, but if you know what the .NET List class is and how to create one, then you’ll be able to follow along with these lectures and demonstrations. 


Demo source code:


Accessing text data from files in C# and .NET is a trivial task involving literally one line of code courtesy of the .NET File class static methods. What isn't trivial is transforming that data into a list of class-based objects. 

Text data is usually formatted as one item, object instance, or record per line. Data elements are usually separated by some special character like a comma or a tab character within each line. When data is formatted like this, each line must contain the same number of elements. Processing each line requires separating the text elements into variables, which may be a simple as an array of string, and mapping those variables onto the relevant class instance properties. If all the class's properties are type string, then a simple assignment will do. Where a class property is not text-based, then some kind of data conversion is required. While you can use or the parse method of a data type like int.parse, if the incoming data is missing or incorrect, it will cause an error. Best practice is to validate data before trying to convert it. This is such a common requirement that non-string data types like int have a try parse method that will return the converted data if successful and a bool value indicating whether data was converted or not. TryParse uses an out parameter to pass the converted value back to the calling code. Validation is essential when ingesting data from any external source, whether a data source or user input. When validating user input, you should provide feedback within the user interface if the data has been entered incorrectly. 


While files are the oldest type of data source, they may no longer be the most common. The explosion of cloud technology in recent years has been accompanied by an increase in data sources and formats. Quickly deployed SQL and No-SQL databases, JSON and XML data formats, and web services mean software needs to be easily and quickly adapted. Having your code's data access methods in separate modules allows you to adapt to changing requirements quickly. The same code modularity principle also applies to the user interface.

About the Author
Learning Paths

Hallam is a software architect with over 20 years experience across a wide range of industries. He began his software career as a  Delphi/Interbase disciple but changed his allegiance to Microsoft with its deep and broad ecosystem. While Hallam has designed and crafted custom software utilizing web, mobile and desktop technologies, good quality reliable data is the key to a successful solution. The challenge of quickly turning data into useful information for digestion by humans and machines has led Hallam to specialize in database design and process automation. Showing customers how leverage new technology to change and improve their business processes is one of the key drivers keeping Hallam coming back to the keyboard. 

Covered Topics