Array of pointers/references to another array

N

naveid

I have an array (List<T>) containing tens of thousands of items. I need
to maintain copies of the array, sorted in a few different ways. I'm
working on Windows Mobile so memory is a constraint. To minimize memory
requirements, in C++, I can have arrays of pointers to the first array
(T*[]). I understand I can do this in C# as well, but the code is
unsafe. Is there a better way of doing this in C#?

Thanks,
Naveed
 
J

Jon Skeet [C# MVP]

I have an array (List<T>)

First thing: List said:
containing tens of thousands of items. I need
to maintain copies of the array, sorted in a few different ways. I'm
working on Windows Mobile so memory is a constraint. To minimize memory
requirements, in C++, I can have arrays of pointers to the first array
(T*[]). I understand I can do this in C# as well, but the code is
unsafe. Is there a better way of doing this in C#?

List<T> is a reference type - just keep a reference to the object.

See http://www.pobox.com/~skeet/csharp/memory.html and
http://www.pobox.com/~skeet/csharp/parameters.html for more on value
types vs reference types.

However, if you're maintaining copies of the list sorted in different
ways, it seems to me that you're not interested in pointers to the
list. After all, the different copies *are* different lists. What's
important is whether the *contents* of the lists are references or
values themselves. That depends on whether T is a value type or a
reference type.

Jon
 
D

Deleo

A List<T> is a generic ArrayList. List<car> can ONLY hold objects of car.
Now if you want to pass List<car> to a method or another place but dont want
to send the entire object, you can use reference. Kinda like pointers in
c++.

Reference is used as followed:
public void sendreference( ref Person p)
{
p.age=555;
}

static void main()
{
Person mel= new Person("Mel",30);
sendreference(ref mel);
}

What happens here is that you pass along the reference to the object mel of
class Person. Any changes you make in the method that recieves the reference
is done to the object itself....So you only pass the "pointer" of the
object.

Hope this helped
 
M

Mattias Sjögren

Now if you want to pass List said:
to send the entire object, you can use reference. [...]
Reference is used as followed:
public void sendreference( ref Person p)

Since List<T> is a reference type you will always just pass around
object references, never "the entire object". No need for the ref
keyword here if you only want to change the content of the list. For
more information see
http://www.yoda.arachsys.com/csharp/parameters.html


Mattias
 
N

Nick Hounsome

public void sendreference( ref Person p)
{
p.age=555;
}

static void main()
{
Person mel= new Person("Mel",30);
sendreference(ref mel);
}

Whilst this is syntactically correct it is a bad example since the use of
reference makes no difference to the behaviour of sendreference whatsoever.

The only point in passing a reference to a reference type is to swap it with
another or make it null:

public void sendreference(ref Person p)
{
if( p.age == 55 )
p = null;
}

This would make "mel" in main null

For the C++ coders the rough equivalents (allowing for -> rather than .)
are:

C# C++
=========================
Person Person*
ref Person Person*&
out Person Person*&
 

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

Top