469,354 Members | 2,485 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,354 developers. It's quick & easy.

List definition

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<TestmyList:
....

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

Is this what is happening?

Thanks,

Tom
Dec 13 '07 #1
1 1944
On Thu, 13 Dec 2007 11:07:38 -0800, tshad <ts***@dslextreme.comwrote:
[...]
// 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<Testclass.
The List<Testclass 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<Testbehavior. I would not bother inheriting List<Test>
unless that extension was specifically needed or desired. You might as
well just use List<Testdirectly instead.
I then saw some code that did something like:

...
List<TestmyList:
...

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<Ttype 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
Dec 13 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Steve | last post: by
57 posts views Thread by Xarky | last post: by
43 posts views Thread by michael.f.ellis | last post: by
5 posts views Thread by Bryan | last post: by
3 posts views Thread by maruf.syfullah | last post: by
6 posts views Thread by Timur Tabi | last post: by
36 posts views Thread by pereges | last post: by
11 posts views Thread by Juha Nieminen | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.