Chapter 4. Working with Objects in C

 < Day Day Up > 

Chapter 4. Working with Objects in C#

Topics in This Chapter

  • Creating Objects: Learn how to use a factory design pattern to create objects.

  • Exception Handling: Effective exception handling requires an understanding of how exceptions are thrown and caught in .NET. Along with an overview of exception handling, this section looks at how to create custom exception objects.

  • Using System.Object Methods: Familiarity with the System.Object methods is necessary if you want to create custom collection classes that implement the standard features found in classes contained in the Framework Class Library.

  • Collection Classes and Interfaces: .NET offers a variety of collection classes for managing multiple objects. This section looks at arrays, hash tables, and stacks, among others. Of particular interest are the 2.0 classes that support generics.

  • Object Serialization: Objects can be converted (serialized) into a binary stream or an XML formatted stream. An example using the binary serializer is presented.

  • Object Life Cycle Management: .NET Garbage Collection automatically removes unreferenced objects. This can produce unwanted results unless measures are taken to ensure that objects are destroyed properly.

The purpose of this chapter is to consider what happens to a class when it becomes an object. This metamorphosis raises some interesting questions: What is the best way to create an object? How do you ensure that an object handles errors gracefully? How do you prevent an object from wasting resources (the dreaded memory leak)? What is the best way to work with groups of objects? How do you dispose of an object? Although these questions are unlikely to keep a developer awake at night, their consideration should lead to a keener insight into class design.

In an attempt to answer these questions, a variety of topics are presented. These include how to create objects using established design patterns; how to implement the System.Object methods on custom classes; how to implement exception handling; how to persist objects using serialization; and how to use collection classes and interfaces to manage groups of objects. The chapter concludes with a look at how to design an object so that it shuts down properly when subject to .NET Garbage Collection.

     < Day Day Up > 

    Core C# and  .NET
    Core C# and .NET
    ISBN: 131472275
    EAN: N/A
    Year: 2005
    Pages: 219 © 2008-2017.
    If you may any questions please contact us: