相当于C#中的结构体

Equivalent to the structure in C#

什么是 C# - 等效于 C 中的以下结构:

struct  netbuf{
unsigned int    maxlen;
unsigned int    len;
char            *buf;
};

我翻译成:

public struct netbuf
{
    public uint maxlen;
    public uint len;
    public string buf;
};

不过好像不太对

我有一个遗留的 C 代码:

datagramm.addr.maxlen   = 0;
datagramm.addr.len      = 0;
datagramm.addr.buf      = (char*) 0;
datagramm.opt.maxlen    = 0;
datagramm.opt.len       = 0;
datagramm.opt.buf       = (char*) 0;

datagramm.udata.len   = sizeof(xliconf);
datagramm.udata.buf   = (char*)&xliconf;

xliconf.ccb_h.source = (uint8)ctrl_ed;
rval = xli_sndudata(ctrl_ed,&datagramm);

xli_sndudata 的声明来自 header:

int xli_sndudata( int , struct t_unitdata *);

struct t_unitdata{
    struct netbuf   addr;     
    struct netbuf   opt;  
    struct netbuf   udata;   
};

上面是struct netbuf。我需要用 C# 翻译该代码。

完全没问题。虽然结构不是 class,但语法非常接近,有一些限制,如下所述:https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/struct

Structs can also contain constructors, constants, fields, methods, properties, indexers, operators, events, and nested types, although if several such members are required, you should consider making your type a class instead.

Structs can implement an interface but they cannot inherit from another struct. For that reason, struct members cannot be declared as protected.

官方的例子和你那里的非常相似。

[StructLayout(LayoutKind.Sequential, Pack = 0)]
public struct netbuf
{
    public ushort maxlen;
    public ushort len;
    public IntPtr buf;
};