6.2. Collections As Method Return Types

Collections provide a way to overcome the limitation that we noted in Chapter 4 about methods being able to return only a single result. If we define a method as having a return type that is a collection type, we can hand back an arbitrary-sized collection to the client code that invokes the method.

In the code snippet shown next for the Course class, we provide a GetRegisteredStudents method to enable client code to request an object reference to the entire collection of Student objects that are registered for a particular course:

public class Course
{
  private List<Student> enrolledStudents;

  // Other details omitted...

  // The following method returns a reference to an entire collection
					// containing however ...

Get Beginning C# 2008 Objects: From Concept to Code now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.