C#+programming03 - Object Etiquette Objects and ToString...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Object Etiquette Objects and ToString C# Programming © Rob Miles 2008 116 Create software which is packaged in secure, interchangeable components. Interfaces let me describe what each component can do. Class hierarchies let me re-use code inside those components. And that is it. Object Etiquette We have considered objects "in the large", in that we know how they can be used to design and implement large software systems. What we need to do now is take a look at some smaller, but very important, issues that relate to how we use objects in our programs. Objects and ToString We have taken it as read that objects have a magical ability to print themselves out. If I write the code: int i = 99; Console.WriteLine(i); This will print out: 99 The integer somehow seems to know how to print itself out. Now it is time to find out how this is achieved, and also how we can give our own objects the same magical ability. It turns out that this is all provided by the "objectness" of things in C#. We know that an object can contain information and do things for us. (in fact we have seen that the whole basis of building programs is to decide what the objects should do and then make them do these things). We have also seen that you can extend a parent object to create a child which has all the abilities of the parent, plus the new ones that we add. Now we are going to see how these abilities of objects are used in making parts of the C# implementation itself work. The Object class When you create a new class this is not actually created from nowhere. It is in fact a child of the object class. In other words, if I write: public class Account { - this is equivalent to writing: public class Account : object { The object class is a part of C#, and everything is a child of the object class. This has a couple of important ramifications: 1. Every object can do what an object can do. 2. A reference to an object type can refer to any class.
Background image of page 1

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

View Full DocumentRight Arrow Icon
Object Etiquette Objects and ToString C# Programming © Rob Miles 2008 117 For the purpose of this part of the notes, the first point is the one which has the most importance here. It means that all classes that are made have a number of behaviours that they inherit from their ultimate parent, the object. If you look inside the actual code that makes an object work you will find a method called ToString . The object implementation of ToString returns a string description of the type of that object. The ToString method The system knows that ToString exists for every object, and so if it ever needs the string version of an object it will call this method on the object to get the text. In other words: object o = new object (); Console.WriteLine(o); - would print out: System.Object You can call the method explicitly if you like: Console.WriteLine(o.ToString()); - and the results would be exactly the same. The nice thing about
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 69

C#+programming03 - Object Etiquette Objects and ToString...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online