mas*_*ani 45 c# c++ tree data-structures
我正在编写一个使用树数据结构的应用程序.我用C++编写它,现在我想用C#编写它.我使用指针来实现树数据结构.C#中还有一个指针吗?使用它安全吗?
Dan*_*ker 41
如果您使用C#(或Java或许多其他语言)实现树结构,则使用引用而不是指针.NB.C++中的引用与这些引用不同.
用法大部分类似于指针,但有垃圾收集等优点.
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);
Run Code Online (Sandbox Code Playgroud)
兴趣点:
*在声明成员时无需修改类型->成员访问的特殊操作员IDisposable)Pra*_*are 33
YES.C#中有指针.
不.他们是不是安全的.
unsafe在C#中使用指针时,实际上必须使用关键字.
static unsafe void Increment(int* i)
{
*i++;
}
Increment(&count);
Run Code Online (Sandbox Code Playgroud)
使用此代码,代码将是安全和清洁.
static void Increment(ref int i)
{
i++;
}
Increment(ref count);
Run Code Online (Sandbox Code Playgroud)