什么是C#等效于C中的以下结构:
struct netbuf{
unsigned int maxlen;
unsigned int len;
char *buf;
};
我将其翻译为:
public struct netbuf
{
public uint maxlen;
public uint len;
public string buf;
};
但似乎不正确
很好。尽管struct不是类,但语法非常接近,但有一些限制,如下所述:https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/struct
结构还可以包含构造函数,常量,字段,方法,属性,索引器,运算符,事件和嵌套类型,尽管如果需要多个此类成员,则应考虑将您的类型设为类。
结构可以实现一个接口,但是它们不能从另一个结构继承。因此,无法将struct成员声明为受保护。
官方示例与您所拥有的非常相似。
unsigned int
通常在C中为2个字节,因此在C#中应为ushort
char*
可以是IntPtr
或byte*
(需要unsafe
)public struct netbuf
{
public ushort maxlen;
public ushort len;
public IntPtr buf;
};