C# Managed Unmanaged code

微笑、不失礼 提交于 2019-12-11 03:39:19

问题


I'm trying to understand managed/unmanaged code as it pertains to structs and classes. I have a struct with a property of another struct but its a pointer declaration as in:

struct StateInfo
{
   Bitboard board;
   StateInfo* previous;
}

I'm converting a C++ project to C#. Anyways, this doesn't work because Bitboard is a class. The error I get is something to the fact that pointers cannot be declared on managed types. If I take out Bitboard from the struct, it's fine. I need it though so I changed Bitboard from a class to a struct, and all is good. I'm not sure what's up? Any ideas?


回答1:


You probably don't even want a struct. Instead:

class StateInfo
{
   Bitboard board;
   StateInfo previous;
}

In C#, a struct is a value type. For instance, int is a struct. They should typically be used for things which are entirely described by their value.




回答2:


I suggest you read about blittability.

Blittable types have the same binary representation in managed and unmanaged code, and you need to represent them the same way if you want pointers to make sense.




回答3:


Essentially, in c# all objects are automatically a pointer and do not need to be released.

Try reading some transitional articles about moving from C++ to C# C++ -> C#: What You Need to Know to Move from C++ to C#



来源:https://stackoverflow.com/questions/6092665/c-sharp-managed-unmanaged-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!