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 开发者_Go百科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?
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.
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.
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#
精彩评论