Using a generic class as a property

  • Thread starter Thread starter Matthias S.
  • Start date Start date
M

Matthias S.

Hi,

I have a class (say Company) which should expose a ReadOnlyCollection
property (say EmployeesCol) hosting userdefined types (Employee).

Since only the internals of the Company class should be allowed to
modify the EmployeesCol, I'd like to expose a read only collection as a
property, so that the users of my Company class can enumerate the
employes, but can't add or remove those.

I think this can be done using the ReadOnlyCollection<T> generic, but I
don't seem to understand the way of using this properly. Do I have to
derive from ReadOnlyCollection<T> or can I just use it in a property
declaration.

It would be nice if somebody could help me getting startet on this one.

Thanks in advance,

Matthias
 
Matthias,

You can use the ReadOnlyCollection<T>. What I would do is keep a
List<T> (where T is Employee) internally in your Company class. When you
expose the ReadOnlyCollection, have it wrap the list you are holding
internally.

Hope this helps.
 
Thanks Nicholas!

Ok, spend some time reading just about anything that deals with
generics, and acutally, they are pretty cool :). Got it done in no time.

Thanks for the fast response!

Matthias
 
Back
Top