有没有指针在C#中像C ++?是否安全? [英] Is there pointer in C# like C++? Is it safe?
问题描述
我正在写一棵树的数据结构工作的应用程序。我用C ++写它,现在我想用C#写。我用指针实现树状数据结构。有没有在C#中太指针?它是安全使用呢?
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?
推荐答案
如果您正在实施在C#中的树状结构(或Java,或许多其他语言)你会使用引用来代替指针。 NB。在C ++中是不一样的,因为这些参考文献引用。
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:
- 不需要用
*
声明成员时, - 没有必要将它们设置为null在修改的类型构造函数(他们已经为空)
- 无特殊
- >
运营商成员访问
<李>无需编写析构函数(尽管查找
的IDisposable
) - 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
)
这篇关于有没有指针在C#中像C ++?是否安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!