C# 中有没有像 C++ 一样的指针?安全吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2333574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there pointer in C# like C++? Is it safe?
提问by masoud ramezani
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++ 编写了它,现在我想用 C# 编写它。我使用指针来实现树数据结构。C#中也有指针吗?使用它安全吗?
采纳答案by Daniel Earwicker
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.
如果您在 C#(或 Java 或许多其他语言)中实现树结构,您将使用引用而不是指针。注意。C++ 中的引用与这些引用不同。
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
)
*
声明成员时无需修改类型- 无需在构造函数中将它们设置为 null(它们已经为 null)
->
会员访问无特殊操作员- 无需编写析构函数(虽然查找
IDisposable
)
回答by kennytm
Is there pointer in C# too?
C#中也有指针吗?
Yes, declared using the syntax int* varName;
.
是的,使用语法声明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 typesso you don't need any pointers. Otherwise, you can box them into a reference.
有一些安全的方法可以在没有指针的情况下构造数据结构。如果节点是类,那么它们将自动成为引用类型,因此您不需要任何指针。否则,您可以将它们装箱到一个 reference 中。
回答by Pratik Deoghare
YES. There are pointers in C#.
是的。C#中有指针。
NO. They are NOTsafe.
否。他们是不是安全的。
You actually have to use keyword unsafe
when you use pointers in C#.
unsafe
在 C# 中使用指针时,实际上必须使用关键字。
For examples look hereand MSDN.
static unsafe void Increment(int* i)
{
*i++;
}
Increment(&count);
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);
回答by Tuomas Hietanen
Yes, there is a pointer: IntPtr
是的,有一个指针: IntPtr
Wikipedia: "which is a safe managed equivalent to int*, and does not require unsafe code"
回答by Jason Short
There is a great series of Data Structures implemented in .Net 2 on MSDN.
在 MSDN 上的 .Net 2 中实现了大量的数据结构。
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.
它们包括二进制搜索树、图形、SkipList、NodeList 等内容的示例代码。代码非常完整,包括许多关于这些结构为何工作的文档页面等。
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.
微软的没有一个使用指针。通常,您永远不需要在 C# 中使用它们。有时使用它们会很好,或者它们只是您从 C++ 中思考的方式。但是您通常可以找到不使用它们的方法。
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.
不为指针使用不安全代码的最大原因是您失去了中等信任合规性。您不能通过单击一次、asp.net 网站等机制运行,Silverlight 也不允许使用它们。坚持使用 refs 和完全托管的概念,以确保您的代码可以在更多地方运行。