471,319 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

Array of pointers/references to another array

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

Feb 21 '06 #1
4 4523
na****@gmail.com wrote:
I have an array (List<T>)
First thing: List<T> is *not* an array. They're different things.
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

Feb 21 '06 #2
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

<na****@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
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

Feb 21 '06 #3
>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. [...]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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Feb 21 '06 #4

"Deleo" <De***@noemail.noemail> wrote in message
news:Om**************@TK2MSFTNGP15.phx.gbl...
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*&
Feb 22 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

19 posts views Thread by Steven T. Hatton | last post: by
5 posts views Thread by TLOlczyk | last post: by
9 posts views Thread by Timothy Madden | last post: by
41 posts views Thread by Berk Birand | last post: by
2 posts views Thread by Neelesh | last post: by
5 posts views Thread by Kiran | last post: by
11 posts views Thread by rayreeves | last post: by
31 posts views Thread by siddhu | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.