C# in Action - Part I: The Basics
- Program Architecture
- Inside the InvestmentController Class
- Inside the InterestInfo Class
- Summary
By now, most people interested in C# are ready to move beyond the plethora of Hello World and FooBar examples across the Net that show what a minimal C# program looks like. To address this issue, I've put together a series of three C# programming articles that pull together language and technology issues that are of practical use. The program itself is a small investment calculation utility named Investment Info—an idea I got from my wife, who is really into learning about that type of thing these days.
Although the program is simple, it highlights several of the interesting aspects of C# and should give you a good feel for how the language is used.
NOTE
If you want some theory and basic syntax information, you can visit my Web site, C# Station, at http://www.csharp-station.com. I've published a free C# tutorial here.
The Investment Info program in this article is a console-based application. It's simple to use, with a couple menus to help calculate how much your money will grow over time at a certain interest rate. Click here for the entire listing. I'll be pulling out bits as we walk the code. And speaking of code....
Program Architecture
The architecture of the Investment Info program consists of two classes: a presentation layer named InvestmentInformation and a management layer named InvestmentController. The InvestmentInformation class presents the menu options, accepts user input, and invokes actions. These actions are invoked upon the InvestmentController class, which holds all information and execution logic. Here are the class definitions:
class InvestmentInfo { // . . . }
and
public class InvestmentController { // . . . }
These classes are declared with the class keyword and a meaningful class name. The InvestmentInfo class has private visibility because no visibility modifier is present in its declaration. However, the InvestmentController class has public visibility, making it accessible to other objects. Within their brackets could be any number of fields (variables), methods (functions or procedures), properties, indexers, or events. A class also may contain other nested objects such as classes, structs, enums, or delegates. Objects are also called reference types, which are allocated on the heap. Some of these terms might be familiar, especially if you're a C or C++ programmer. I'll discuss several of them later in this article and in subsequent articles.
Both of these classes are in the same namespace, InvestmentInfo. A namespace helps organize code and avoid name clashes between program elements. Here's what the namespace declaration looks like:
using System; namespace InvestmentInfo { public class InvestmentController . . . }
Namespaces are declared with the namespace keyword and a meaningful namespace name. At the top of the previous example is a using directive. This permits the code following it to reference members in the System namespace without qualifying each reference with "System." It's a shortcut.