C# 托管非托管代码 [英] C# Managed Unmanaged code

查看:57
本文介绍了C# 托管非托管代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解托管/非托管代码,因为它与结构和类有关.我有一个具有另一个结构属性的结构,但它的指针声明如下:

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;
}

我正在将 C++ 项目转换为 C#.无论如何,这不起作用,因为 Bitboard 是一个类.我得到的错误是指针不能在托管类型上声明.如果我从结构中取出 Bitboard,那就没问题了.我需要它,所以我将 Bitboard 从一个类更改为一个结构,一切都很好.我不知道怎么回事?有什么想法吗?

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?

推荐答案

您可能甚至不需要 struct.相反:

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

class StateInfo
{
   Bitboard board;
   StateInfo previous;
}

在 C# 中,struct 是一种值类型.例如,int 是一个 struct.它们通常应该用于完全由其价值描述的事物.

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.

这篇关于C# 托管非托管代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆