Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, guys, i have this structs in c++ and i wnat to convert this to c#, how i can code more close in c#?
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:

C#
[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;
    }
Posted
Updated 7-Nov-18 22:20pm
v2

1 solution

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#,
C++
template<typename T>
struct hashtable_iterator
{
    hash_node<T>* mpNode;
    hash_node<T>** mpBucket;
};
This can be rewritten to this one,
C#
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[^]
 
Share this answer
 
Comments
CesatAGS 8-Nov-18 12:56pm    
thx i just have issue about this model

template<typename T>
struct hash_node
{
pares<t> mValue;
hash_node<t>* mpNext;
};

my tried C#
struct hash_node<t>
{
pair<t> mValue;
hash_node<t> mpNext;// get error on this. hash_node<t>.mpNext' of type 'Program.hash_node<t>' causes a cycle in the struct layout
}
Afzaal Ahmad Zeeshan 8-Nov-18 14:39pm    
Basically that is because, structures are not like reference-types, their sizes have to be calculated for the layout, and that means that the members of the struct also need to be calculated, and that is a recursive cycle, a never ending one. That is why, it is not recommended to contain the struct within a struct. For self-containing, consider classes.

See here on SO.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900