2.7. The Class Constructor

Our WordCount program requires that the user provide a file name. Optionally, the user can turn on several options, such as generating trace output and timing diagnostics. The Main() entry point processes the command-line options. That done, it must create a WordCount object and pass the file name and possible options to that object before asking the object to process the file.

One solution, of course, is to create a WordCount object with the default values and then reset the values the user passed in through the command-line options. The problem with this strategy is that it requires two steps, and the crucial second one is something that the user might overlook.

The class constructor is a mechanism by which the user ...

Get C# Primer: A Practical Approach now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.