List definition

  • Thread starter Thread starter tshad
  • Start date Start date
T

tshad

I have the following code:
***************************************
public class Test
{
....

public static TestCollection MyNewCollection()
{
}
}

// Define Collections here
public class TestCollection : List<Test>
************************************

What is this doing here?

I assume that I have a class of Test and the last line allows me to make
class that is just a collection (array) of objects of type Test.

Is that what is happening here?

I then saw some code that did something like:

....
List<Test> myList:
....

Which would define "myList" as a collection of objects of type Test?

Is this what is happening?

Thanks,

Tom
 
[...]
// Define Collections here
public class TestCollection : List<Test>
************************************

What is this doing here?

I assume that I have a class of Test and the last line allows me to make
class that is just a collection (array) of objects of type Test.

Is that what is happening here?

The last line creates a new class that _inherits_ the List<Test> class.
The List<Test> class itself is a concrete version of the generic List<T>
class, which is not exactly an array. It's sort of a merged version of
Array and ArrayList, with typed elements like an Array has, but with the
dynamic features of ArrayList.

By inheriting it, the TestCollection class can add its own functionality
to the base List<Test> behavior. I would not bother inheriting List<Test>
unless that extension was specifically needed or desired. You might as
well just use List said:
I then saw some code that did something like:

...
List<Test> myList:
...

Which would define "myList" as a collection of objects of type Test?

Yes. It declares myList as a specific kind of collection of objects of
type Test. .NET has a wide variety of collection types, and the generic
List<T> type is just one of them.

Perhaps more important for you to understand is that the "<T>" part is not
unique to collections. The collection classes happen to be one of the
more prominent uses of generics in the framework, but generics are not
used solely for collections. So in that example, it's the "List" part
that is the important thing with respect to the type being a collection;
the "<Test>" part is just part of the way you declare that particular kind
of collection (that is, using a generic class).

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top