Replacement for CTypedPtrList

  • Thread starter Thread starter Robert Wells
  • Start date Start date
R

Robert Wells

I am converting a C++ project management program I wrote to C#. Each Protask
obect represent a task and includes two CTypedPtrList objects: the lists of
pointers to the prerequisite tasks and dependent tasks. There doesn't seem
to be an equivalent to the CTypedPtrList in C#. I'm not sure if ArrayList or
a List(T) generic would best fill the requirement. I am guessing that
regardless of which I use, I will need to use the unsafe keyword to use
pointers. Would a List(T) generic list be safer than an ArrayList?
Any thoughts would be most appreciated.
Bob
 
I'm not familiar with CTypedPtrList, but from your description it
sounds like List<T>, where T is a class (not a struct):

* each item is a reference
* each item is strongly typed

i.e.
public sealed class Protask {
private readonly List<Protask>
preRequisites = new List<Protask>(),
dependents = new List<Protask>();

public List<Protask> PreRequisites { get { return
preRequisites; } }
public List<Protask> Dependents { get { return
dependents; } }

}

Of course, you might want to encapsulate the lists a bit more if you
want editing one to automatically update the other end (if you see
what I mean).

Marc
 
Hello Marc,

Thank you for your reply. Can T be a pointer to a class?
 
Robert,

You don't have pointers to classes in .NET, you have references (there
is a difference). But yes, T can be a reference type, where the List<T> is
the list of reference types. This means that when you access an item from
the List<T>, you will get a reference that points to the same item that the
reference in List<T> points to (whereas if T was a struct, you would get a
copy on return because of the copy semantics of structures on assignment).
 
Hello Nicholas,

I'm still not sure if I understand this correctly.
Does a List<T> object holds a bunch of objects of type T or
Does it only hold the references (addresses?) of those objects?

I certainly don't want each task to hold copies of all its prerequisites and
dependent tasks.

Thanks,
Bob


Nicholas Paldino said:
Robert,

You don't have pointers to classes in .NET, you have references (there
is a difference). But yes, T can be a reference type, where the List<T>
is the list of reference types. This means that when you access an item
from the List<T>, you will get a reference that points to the same item
that the reference in List<T> points to (whereas if T was a struct, you
would get a copy on return because of the copy semantics of structures on
assignment).

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Robert Wells said:
Hello Marc,

Thank you for your reply. Can T be a pointer to a class?
 
Robert Wells said:
I'm still not sure if I understand this correctly.
Does a List<T> object holds a bunch of objects of type T or
Does it only hold the references (addresses?) of those objects?

I certainly don't want each task to hold copies of all its prerequisites and
dependent tasks.

Assuming T is a reference type (e.g. a class) then it only holds
references.
 
As Jon has stated - references; that is why I added my "where T is a
class (not a struct)" caveat to my original reply.

In .NET, and very unlike C++, there is a large and fundamental
difference between "struct" and "class". A struct (examples: int,
float, DateTime, etc) follows value-type semantics, and will copy
itself (memcopy) sooner than you can glance at it - i.e.

int x = 5;
int y = x; // [memcopy of the struct]

Because of this, structs are almots always immutable (non-editable
once created), because it just gets too confusing to think about them
otherwise.

In contrast, classes have reference-type semantics. "new" creates a
new object on the managed heap, and the field/variable just holds the
referenece (think: pointer). Most .NET types are classes. Assignment
of variables copies *the reference*, not the object:

Foo x = new Foo();
Foo y = x; // reference value is copied, both x & y point to same Foo


Marc
 

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