Free Programming E-Books
Free download ebooks on computer and programming

Free ebook "Beginning C# Objects: From Concepts to Code" Sample Chapters

Beginning C# Objects: From....
Free Download Chapter 3: Objects and Classes
Download chapter
Beginning C# Objects: From....
Free Download Chapter 6: Collections of Objects
Download chapter

Beginning C# Objects: From Concepts to Code is a comprehensive yet approachable guide for anyone interested in learning the C# language, beginning with the basics.

To begin, this book addresses the two fundamental concepts that programmers must grasp in order to write a professional object-oriented C# application: the nature and characteristics of objects, and the structure that allows you to take best advantage of C#'s object-oriented makeup.

In addition to the basics of C# syntax, this book introduces object terminology-teaching you how to think in terms of objects-and provides an introduction of object modeling, illustrating how to translate an object model into C# code with ease.

For folks who have already attempted to learn C# but are still having trouble mastering C#'s object-oriented nature, the case study format of this book will be invaluable in showing you how to translate a UML model into a fully-functional C# application.

An overwhelming number of programmers are now moving to C# as their language of choice for developing powerful, maintainable, scalabe software applications. Whether you're learning C# as your first programming language, moving to C# from a non-object-oriented language, or have previously programmed with C# but still feel unsure when it comes to object aspects, this book is a perfect fit for you.

< < prev next > >

Collections of Objects

YOU LEARNED ABOUT the process of creating objects based on class definitions, a process known as instantiation, in Chapter 3. When we're only creating a few objects, we can afford to declare individualized reference variables for these objects: Students s1, s2, s3, perhaps, or Professors profA, profB, profC. But, at other times, individualized reference variables are impractical.

  • Sometimes, there will be too many objects, as when creating Course objects to represent the hundreds of courses in a university's course catalog.
  • Worse yet, we may not even know how many objects of a particular type there will be in advance. With our Student Registration System, for example, we may create a new Student object each time a new student logs on for the first time.

Fortunately, OOPLs solve this problem by providing a special category of object called a collection that is used to hold and organize other objects.

In this chapter, you'll learn about

  • The properties and behaviors of some common collection types
  • How collections enable us to model very sophisticated real-world concepts or situations
  • How we can define our own collection types

What Are Collections?

We'd like a way to gather up objects as they are created so that we can manage them as a group and operate on them collectively, along with referring to them individually when necessary. For example:

  • A Professor object may need to step through all Student objects registered for a particular Course that the professor is teaching in order to compute their grades.
  • The Student Registration System (SRS) application as a whole may need to step through all of the Course objects in the current schedule of classes to determine which of them don't yet have any students registered for them, possibly to cancel these courses.

We use a special type of object called a collection to group other objects. A collection object can hold/contain multiple references to some other type of object. Think of a collection like an egg carton, and the objects it holds like the eggs! Both are objects, but with decidedly different properties.

Because collections are implemented as objects, this implies that

  • Collections must be instantiated before they can first be used.
  • Collections are defined by classes that in turn define methods for "getting" and "setting" their contents.
  • By virtue of being objects, OO collections are encapsulated, and hence take full advantage of information hiding.
Let's discuss each of these three matters in turn.

Collections Must Be Instantiated Before They Can First Be Used

We can't merely declare a collection:
CollectionType c;
For example:
ArrayList c;
All this does is to declare a reference variable of type CollectionType. Until we "hand" c a CollectionType object to refer to, c is said to have the value null.

ArrayList is one of C#'s predefined collection types, defined by the .NET Framework Class Library (FCL).We'll introduce the ArrayList class in this chapter, and we'll then go into greater detail about ArrayLists, along with several other collection types, in Chapter 13.

We have to take the distinct step of using the new operator to actually create an empty CollectionType object in memory, as follows:

c = new CollectionType();
For example:
c = new ArrayList();
Think of the newly created CollectionType object as an empty "egg carton," and the reference variable c as the handle that allows us to locate and access (reference) this egg carton whenever we'd like.

 
    
190