POINTERS

  • Thread starter Thread starter Pohihihi
  • Start date Start date
P

Pohihihi

Hello,

How can I use pointers in c#. I am trying to create a link list.

Cheers,
Po
 
All reference types are already pointers. Rather than setting the "next" field to a point, you just set it equal to the actual object that would be next.

Hello,

How can I use pointers in c#. I am trying to create a link list.

Cheers,
Po
 
"Pohihihi" <[email protected]> a écrit dans le message de (e-mail address removed)...

| How can I use pointers in c#. I am trying to create a link list.

As Peter has said, all references in C# are really pointers under the skin,
you just don't need to dereference them.

..NET 2.0 has a LinkedList<T> class already, so you may not need to do your
own unless you want to do it as a learning experience.

Otherwise, rolling your own is as simple as this :

public class Node
{
private object data;

private Node next;

public object Data
{
get { return data; }
set { data = value; }
}

public Node Next
{
get { return next; }
set { next = value; }
}

...
}

public class LinkedList
{
private Node first;

private Node last;

...

public void Add(Node node)
{
if (first == null)
{
first = node;
last = first;
}
else
{
last.Next = node;
last = node;
}
}
}

.... or something like that.

Joanna
 
The point is that you don't need pointers to implement linked lists in
managed code. Unsafe won't be of great help either, the types a pointer in
C# can point to is somewhat limited, for instance you cannot point to
references or structures that contain references and that's what you need to
implement linked lists of arbitrary managed types.

Willy.

| Hi Pohihihi,
|
| Did you check "unsafe" :
|
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfUnsafe.asp
|
| Regards,
| Bart
|
| http://www.xenopz.com/blog/bartdeboeck/
|
 
The .Net 2.0 platform has a very nice Generic LinkedList class which can be
used straight out of the box, BTW.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.
 
Hello,

How can I use pointers in c#. I am trying to create a link list.

Cheers,
Po
 

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