是否有可能在下面的例子中我是拳擊一個struct
名爲分數,到隨後拆箱的struct
例如分子的特定成員?的Unbox具體結構成員
using system;
struct fraction
{
public int numerator;
public int denominator;
}
class Program
{
static void Main()
{
fraction f1;
f1.denominator = 100;
f1.numerator = 10;
object obj = f1;
// initializing f2.
fraction f2 = new fraction();
// Or can unbox the obj to the f2 like this.
f2 = (fraction)obj;
// But if i want to only unbox the numerator member of the struct fraction boxed inside the obj Something like this will not work
f2.numerator = (fraction)obj.numerator;
}
}
你可以使用'((分數)obj).numerator',不是? –
@GrantWinney抱歉不明白你的意思,你能解釋一下嗎?編輯:現在謝謝你的作品,你可以解釋爲什麼一個額外的括號是必要的? – Johnson
我的意思是說,在一個步驟中取消裝箱和訪問該字段相對容易。 –