Understanding Objects and Collections
Hour 3: Understanding Objects and Collections
In Hour 1, "Jumping in with Both Feet: A Visual C# .NET Programming Tour," you were introduced to programming in Visual C# by building a Picture Viewer project. You spent Hour 2, "Navigating Visual Studio .NET," digging into the IDE and learning skills critical to your success with Visual C#. In this hour, you're going to start learning about some important programming concepts, namely objects.
The term object as it relates to programming may have been new to you prior to reading this book. The more you work with Visual C# .NET, the more you'll hear about objects. Visual C# .NET is a true object-oriented language. This hour isn't going to discuss object-oriented programming in great detailobject-oriented programming is a very complex subject and is well beyond the scope of this book. Instead, you'll learn about objects in a more general sense.
Everything you use in Visual C# .NET is an object, so understanding this material is critical to your success with Visual C# .NET. For example, forms are objects, as are the controls you place on a form; pretty much every element of a Visual C# .NET project is an object and belongs to a collection of objects. All objects have attributes (called properties), most have methods, and many have events. Whether you're creating simple applications or building large-scale enterprise solutions, you must understand what an object is and how it works. In this hour, you'll also learn what makes an object an object, and you'll learn about collections.
The highlights of this hour include the following:
Getting and setting properties
Understanding method dynamism
Writing object-based code
Using the Object Browser
If you've listened to the programming press at all, you've probably heard the term object-oriented, and perhaps words such as polymorphism and inheritance. I won't cover all the details of object-oriented programming (OOP) theory in this book: You'll be introduced to object-oriented programming in this book, but if you want to take your programming skills to a more advanced level, you should progress to a book dedicated to the subject of OOP after you've completed this one.
Object-oriented programming has been a technical buzzword for quite some time. Almost everywhere you lookthe Web, publications, booksyou read about objects. What exactly is an object? Strictly speaking, it is a programming structure that encapsulates data and functionality as a single unit. When data and functionality are encapsulated, the only public access to the data and functions is through the programming structure's interfaces (properties, methods, and events). In reality, the answer to this question can be somewhat ambiguous because there are so many types of objectsand the number grows almost daily. However, all objects share specific characteristics, such as properties and methods.
The most commonly used objects in Windows applications are the form object and the control object. Earlier hours introduced you to working with forms and controls and showed you how to set form and control properties. In your Picture Viewer project from Hour 1, for instance, you added a picture box and two buttons to a form. Both the PictureBox and the Button control are control objects, but each is a specific type of control object. Another, less technical example uses pets. Dogs and cats are definitely different entities (objects), but they both fit into the category of Pet objects. Similarly, text boxes and buttons are each a unique type of object, but they're both considered a control object. This small distinction is important.