Is there pointer in C# like C++? Is it safe?

90,753

Solution 1

If you're implementing a tree structure in C# (or Java, or many other languages) you'd use references instead of pointers. NB. references in C++ are not the same as these references.

The usage is similar to pointers for the most part, but there are advantages like garbage collection.

class TreeNode
{
    private TreeNode parent, firstChild, nextSibling;

    public InsertChild(TreeNode newChild)
    {
        newChild.parent = this;
        newChild.nextSibling = firstChild;
        firstChild = newChild;
    }
}

var root = new TreeNode();
var child1 = new TreeNode();
root.InsertChild(child1);

Points of interest:

  • No need to modify the type with * when declaring the members
  • No need to set them to null in a constructor (they're already null)
  • No special -> operator for member access
  • No need to write a destructor (although look up IDisposable)

Solution 2

YES. There are pointers in C#.

NO. They are NOT safe.

You actually have to use keyword unsafe when you use pointers in C#.

For examples look here and MSDN.

static unsafe void Increment(int* i)
{
    *i++;
}

Increment(&count);

Use this instead and code will be SAFE and CLEAN.

static void Increment(ref int i)
{
    i++;
}

Increment(ref count);

Solution 3

Is there pointer in C# too?

Yes, declared using the syntax int* varName;.

Is using of that safe?

No pointers are not safe.

There are safe ways to construct a data structure without pointers. If the nodes are classes, then they'll automatically be reference types so you don't need any pointers. Otherwise, you can box them into a reference.

Solution 4

Yes, there is a pointer: IntPtr

Wikipedia: "which is a safe managed equivalent to int*, and does not require unsafe code"

Solution 5

There is a great series of Data Structures implemented in .Net on Microsoft Docs.

Data Structures Overview

They include sample code for things like Binary Search Tree, Graph, SkipList, NodeList, etc. The code is quite complete and includes a number of pages of docs about why these structures work, etc.

None of the ones from Microsoft use pointers. In general, you never NEED to use them in C#. There are times when using them would be nice, or they are just the way you think from C++. But you can usually find a way not to use them.

The biggest reasons why not to use unsafe code for pointers is that you lose Medium Trust compliance. You can't run through mechanisms like click once, asp.net websites, and Silverlight doesn't allow them either. Stick with refs and fully managed concepts to ensure your code can run in more places.

Share:
90,753
masoud ramezani
Author by

masoud ramezani

I'm a Software Developer. :)

Updated on November 03, 2020

Comments

  • masoud ramezani
    masoud ramezani over 3 years

    I'm writing an application that work with a tree data structure. I've written it with C++, now i want to write it by C#. I use pointers for implementing the tree data structure. Is there a pointer in C# too? Is it safe to use it?

  • Joachim Sauer
    Joachim Sauer about 14 years
    Important difference between pointers and references: The latter doesn't support pointer arithmetic!
  • Daniel Earwicker
    Daniel Earwicker about 14 years
    @Joachim - let's all pray he doesn't normally use pointer arithmetic in his tree structures.
  • Tuomas Hietanen
    Tuomas Hietanen about 14 years
  • Wisteso
    Wisteso over 8 years
    This answer does not address the actual question. Came here from a google search, and fortunately others had actually answered the question...
  • Christophe
    Christophe over 6 years
    Interesting answer. Unfortunately, the link to the yes part seems broken (i.e. Download of full vs 2005 doc instead of specific language feature). The boxing/unboxing gives the impression that it's safe; why isn't it ?
  • kennytm
    kennytm over 6 years
    @Christophe Thanks, fixed the broken links. Also clarified the wordings, that boxing/unobxing are safe ways to avoid the unsafe pointers.
  • Shadi Alnamrouti
    Shadi Alnamrouti about 5 years
    The link is no longer supported
  • Jason Short
    Jason Short about 2 years
    Fixed the link, hope that helps someone.