.. index:: value type reference object struct .. .. index:: class; plan classes and methods .. plan problem split into classes .. .. _plan-classes: .. Planning A Class Structure .. ============================== .. The Console input/output interchange below illustrates .. an idea for a skeleton of a text (adventure?) game. .. It could be the basis of a later group project. It does not .. have much in it yet, but it can be planned in terms of classes. .. Classes with instances correspond to nouns you would be using, .. particularly nouns used in more than one place with different .. state data being remembered. .. Verbs associated with nouns you use tend to be methods. .. Think how you might break this down, looking at what is happening .. in the sequence below. .. The parts appearing after the '>' prompt are entries by the user. .. Other lines are computer responses: .. .. code-block:: none .. Welcome to Loyola! .. This is a pretty boring game, unless you modify it. .. Type 'help' if you need help. .. You are outside the main entrance of the university that prepares people for .. extraordinary lives. It would help to be prepared now.... .. Exits: east south west .. > help .. You are lost. You are alone. .. You wander around at the university. .. Your command words are: .. help go quit .. Enter .. help command .. for help on the command. .. > help go .. Enter .. go direction .. to exit the current place in the specified direction. .. The direction should be in the list of exits for the current place. .. > go west .. You are in the campus pub. .. Exits: east .. > go east .. You are outside the main entrance of the university that prepares people for .. extraordinary lives. It would help to be prepared now.... .. Exits: east south west .. > go south .. You are in a computing lab. .. Exits: north east .. > go east .. You are in the computing admin office. .. Exits: west .. > bye .. I don't know what you mean... .. > quit .. Do you really want to quit? yes .. Thank you for playing. Good bye. .. Think and discuss how to organize things first.... .. The different parts of a multi-class project interact through their public methods. .. Remember the two roles of writer and consumer. The consumer needs good documentation .. of how to use (not implement) these methods. These methods that allow the .. interaction between classes provide the *interface* between classes. Unfortunately .. "interface" is used in more than one way. Here it means publicly specified ways .. for different parts to interact. .. As you think how to break this game into parts (classes), .. also think how the parts interact (public methods). .. This is a good place for the start of a class discussion. .. If the plan is to discuss it in class, *wait* before looking at .. the code that generated the exchange above, in the .. project folder :repsrc:`cs_project1`. .. The code uses many of the topics discussed so far in this book. .. We will add some features from another meaning of :ref:`Interface`, .. and discuss the revision in project .. :repsrc:`csproject_stub` (no 1). You *might* use this version .. as a basis of a project. .. _structs-and-classes: Structs (Optional) ================================ .. abstract class .. https://dotnettutorials.net/lesson/file-handling-in-csharp/ .. In C#, The System.IO namespace contains the required classes used to handle the input and output .. streams and provide information about file and directory structure. The parent class of .. file processing is Stream. Stream is an abstract class used as the parent of the .. classes that implement the necessary operations. C# has an alternate syntax to a class: a *struct*. Everything we have said so far about classes applies to structs also! In fact you could change ``class`` into ``struct`` in the heading for Rational, and it would become a ``struct``, with no further code changes in any of the code we have written!:: public struct SomeMath { // ... } So why the distinction? We have mentioned that new objects created in a class are accessed indirectly via a reference, as with an array. As a general category, they are called *reference objects*. We distinguished the types ``int`` and ``double`` and ``bool``, where the actual value of the data is stored in the space for a variable of the type. They are *value types*. A struct is also a value type. In practice this is efficient for small objects of a fixed size. We made Rational a class because you have already seen the class construct with ``static`` entries, and classes are more generally useful. In fact being a ``struct`` would be a good choice for Rational, since it only contains two integers. Its size is no more than one ``double``. The behavior of a Rational is the same either way, because it is immutable. If we allowed mutating methods, then a class version and a struct version would not behave the same way, due to the fact the reference types can have aliases, and value types cannot. There are some more complicated situations where there are further distinctions between classes and structs, but we shall not concern ourselves with those fine advanced points in this book.