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;
}
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[
^]