2

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?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
David
  • 131
  • 3
  • 6

3 Answers3

2

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.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
0

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#

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Craig White
  • 13,492
  • 4
  • 23
  • 36
  • as you'll notice if you read that article, there is no such thing as "C#.NET". The language is named "C#". I'll also note for readers that the article is a decade old. Both languages have evolved in the past ten years. – John Saunders May 23 '11 at 04:08
0

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.

user541686
  • 205,094
  • 128
  • 528
  • 886