Class it had a single string property for a name with

Info icon This preview shows pages 150–152. Sign up to view the full content.

View Full Document Right Arrow Icon
class, it had a single string property for a Name . With the Administrator , you can independently get and set the Title , Forename , and Surname . We then provided a special read-only property that returns a single formatted string for the whole Name . It uses a framework class called StringBuilder to assemble the name from the individual components as efficiently as possible. AppendWithSpace is a utility function that does the actual work of concatenating the substrings. It works out whether it needs to append anything at all using a static method on string that checks whether it is null or empty, called IsNullOrEmpty ; finally, it adds an extra space to separate the individual words. To do the roll call we want to write some code such as that in Example 4-16 . Example 4-16. Using the Administrator class static void Main(string[] args) { FireStation station = new FireStation(); // A reference to Joe, Harry's number one Firefighter joe = new Firefighter { Name = "Joe" }; // A reference to Bill, the trainee FirefighterBase bill = new TraineeFirefighter { Name = "Bill" }; // Harry is back FireChief bigChiefHarry = new FireChief { Name = "Harry"}; // And here's our administrator - Arthur Administrator arthur = new Administrator { Title = "Mr", Forename = "Arthur", Surname = "Askey" }; station.ClockIn(joe); station.ClockIn(bill); station.ClockIn(bigChiefHarry); station.ClockIn(arthur); 126 | Chapter 4: Extensibility and Polymorphism
Image of page 150

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
station.RollCall(); Console.ReadKey(); } When you are designing a class framework it can often be a good idea to write some example client code. You can then ensure that your design is a good abstract model while supporting clean, simple code at point- of-use. Clearly, we’re going to need a FireStation class that is going to let our administrators and firefighters ClockIn (registering their presence in the station), and where we can do a RollCall (displaying their names). But what type is that ClockIn function going to take, given that we haven’t specified any common base class that they share? All Types Are Derived from Object .NET comes to our rescue again. It turns out that every type in the system is derived from Object . Every one—value types ( struct ) and reference types ( class ) alike, even the built-in types such as Int32 . It is easy to see how that would work for a class declaration in C#. If you don’t specify a particular base class, you get Object by default. But what about a struct , or enum , or the built-in types; what happens if we try to talk to them through their Object “base class”? Boxing and Unboxing Value Types Let’s give it a try. This code snippet will compile and work quite happily: // Int variable int myIntVariable = 1; object myObject = myIntVariable; What happens under the covers is that the runtime allocates a new object and puts a copy of the value inside it. This is called boxing , and, as you might expect given that it involves allocating objects and copying values, it is relatively expensive when compared to a straightforward assignment.
Image of page 151
Image of page 152
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern