如何在C#中使用指针如C ++ [英] How use pointer in C# like C++

查看:158
本文介绍了如何在C#中使用指针如C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我在c ++中有这个结构,我想把它转换成c#,我怎样才能在c#中编写更接近的代码?

Hi, guys, i have this structs in c++ and i wnat to convert this to c#, how i can code more close in c#?

template<typename T>
	struct pares
	{
		DWORD64 first;
		T* second;
	};
	template<typename T>
	struct hash_node
	{
		pares<T> mValue;
		hash_node<T>* mpNext;
	};

	template<typename T>
	struct hashtable
	{
		DWORD64 vtable;
		hash_node<T>** mpBucketArray;
		unsigned int mnBucketCount;
		unsigned int mnElementCount;
		//...
	};

	template<typename T>
	struct hashtable_iterator
	{
		hash_node<T>* mpNode;
		hash_node<T>** mpBucket;
	};





我尝试过:





What I have tried:

[StructLayout(LayoutKind.Sequential)]
    public unsafe struct pair<T>
    {

        public Int64 first;
        public  T second;
    }

    public class hash_node<T>
    {
        public pair<T> mValue = new pair<T>();
        public hash_node<T> mpNext;
    }

    public class hashtable<T>
    {
        public Int64 vtable;
        public hash_node<T>[] mpBucketArray;
        public int mnBucketCount;
        public int mnElementCount;
    }
    public class hashtable_iterator<T>
    {
        public hash_node<T> mpNode;
        public hash_node<T> mpBucket;
    }

推荐答案

如果你想使用C#,那么你必须将所有指针都归功于C ++。 C#的核心概念是使您拥有一个托管环境,您只需编写业务逻辑,而不必管理低级内存和硬件。 C#本身足以为您管理引用,清除它们,传递值的引用。



现在在你的C ++代码中,你有4种结构类型,在C#中你只有一个结构。即使在这种情况下,您也滥用了 unsafe 范围。这不是一个不安全的上下文意味着什么,这意味着你可以直接使用那里的本地指针,你负责范围本身。然而,您正试图在那里使用托管指针包含类型 - IntPtr IntPtr 不是C ++所拥有的 int * ,要理解这一点,需要您理解为什么管理语言曾经创建。



除此之外,您的代码在任何方面都与C ++不同。 C ++中的结构必须用C#中的struct替换。虽然与C#中的value-type和reference-type相比,value-type和reference-type的概念是巨大的,但让我们暂时忽略这个事实。如果你想在.NET框架中使用C ++,那么考虑一下.NET的C ++实现,即使在这种情况下你应该更喜欢托管引用( ^ operator [ ^ ]),而不是原生指针。



就像一般情况一样,让我告诉你如何用C#重写C ++代码,

If you want to use C# because, then you have to leave all that pointer luxury with the C++. The core concept of C# was to enable you to have a managed environment, where you only had to write the business logic instead of having to manage the low-level memory and hardware as well. C# itself is well enough to manage the references for you, clear them out, pass the references-by-value.

Now in your C++ code, you have 4 struct types, and in C# you only have one struct. Even in that case, you misused the unsafe scope. That is not what an unsafe context means, it means that you can directly consume the native pointers over there and you are responsible for the scope itself. Whereas, you are trying to utilize the managed pointer-containing type over there—IntPtr. IntPtr is not what C++ has as int*, and to understand this, requires that you understand why managed languages were ever created.

Apart from that case, your code is not identical to C++ in any way. The struct in C++, must be replaced with struct in C#. Although the concepts of value-type and reference-type is vast when compared to value-type and reference-type in C#, but let's ignore the fact for a while. If you want to utilize C++ in .NET framework, then consider the .NET's implementation of C++, even in that case you should prefer the managed reference (^ operator[^]), instead of the native pointers.

Just as a general case, let me tell you how you can rewrite the C++ code in C#,
template<typename T>
struct hashtable_iterator
{
    hash_node<T>* mpNode;
    hash_node<T>** mpBucket;
};

这可以改写为这个,

This can be rewritten to this one,

struct hashtable_iterator<T> {
    hash_node<T> mpNode;
    List<hash_node<T>> mpBucket;
}

在上面的代码中,您首先忽略了需要将指针放在那里的事实。运行时本身负责通过引用传递类型,而不是复制 - 这就是指针的含义。第二个成员是一个指向指针的指针,我们可以使用 List< T> array [] ,这取决于你的用例。如果指针的目的是制作列表,则可以添加额外的列表< T> - 这不太可能因为您的成员的命名建议。



可选建议:您也应该考虑在C ++代码中使用智能指针!



请参阅这些链接,

c ++ cli - IntPtr vs C ++指针 - 堆栈溢出 [ ^ ]

IntPtr。添加(IntPtr,Int32)方法(系统)| Microsoft Docs [ ^ ]

智能指针(现代C ++)| Microsoft Docs [ ^ ]

In the code above, you first ignore the fact that you require to have the pointer there. Runtime itself takes care of passing around the types, by reference, not copying—that is what your pointer means. Second member is a pointer-to-pointer, we can use a List<T> or an array[], that depends on your use case. You can add an extra List<T>, if your purpose of a pointer was to make a list—which is unlikely as the naming of your members suggest.

An optional advice: You should consider using smart pointers in your C++ code too!

Please see these links,
c++ cli - IntPtr vs C++ pointers - Stack Overflow[^]
IntPtr.Add(IntPtr, Int32) Method (System) | Microsoft Docs[^]
Smart Pointers (Modern C++) | Microsoft Docs[^]


这篇关于如何在C#中使用指针如C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆