对象——比;结构体

本文关键字:结构体 对象 | 更新日期: 2023-09-27 17:49:45

如何将对象转换为结构体,反之亦然?

public void myMethod1(object myInputObject, out string myOutputString) 
{
   myInputObject = null;
   myOutputString = "";
  //Convert object into a struct, then do something
}

对象——比;结构体

定义一个结构体并在方法中设置字段值

我想你想要这样的东西:(伪代码)

class myclass
{
public int a;
public float b;
}
struct somestruct
{
somestruct(int a, float b)
{
   this.a = a;
   this.b = b;
}
int a;
float b;
}

myclass mc = new myclass();
mc.a = 125;
mc.b = 12.5;
somestruct s = new somestruct(mc.a, mc.b); //all fields of mc are now in struct somestruct