使用 c# 我想在一个结构中设置变量,该结构是类的成员,从该类中。对于 c# 来说还很陌生。帮助表示赞赏。
class myclass
{
public struct mystruct
{
public int something;
}
public void init()
{
mystruct.something = 20; // <-- this is an error
}
static void Main(string[] args)
{
myclass c = new myclass();
c.init();
}
}
Run Code Online (Sandbox Code Playgroud)
错误:“非静态字段、方法或属性 myclass.mystruct.something 需要对象引用”
mystruct是类中的一种类型,但您没有任何具有该类型的字段:
class myclass
{
public struct mystruct
{
public int something;
}
private mystruct field;
public void init()
{
field.something = 20; // <-- this is no longer an error :)
}
static void Main(string[] args)
{
myclass c = new myclass();
c.init();
}
}
Run Code Online (Sandbox Code Playgroud)