我有許多類似的方法具有不同的參數。我試圖找到一個好的和簡單的重構解決方案,但沒有結果。我可以使用一個參數對象,但我想我需要創建另一個輔助類,它會使一切變得更加複雜和慢。如何避免使用不同參數的方法
void Foo(ref int test2)
{
Goo(test2);
}
void Foo(ref long test2)
{
Goo(test2);
}
void Foo(ref double test2)
{
Goo(test2);
}
void Foo(ref string test2)
{
if (!test2.IsNull())
test2 = "sth";
Goo(test2);
}
你在尋找* generics *嗎? 'void Foo(ref T test2)' –
如果你提供一個更復雜的例子,它會有所幫助。我們不知道「Goo」是做什麼的,或者爲什麼你的前三種方法接受'ref'參數,儘管沒有改變它們。 –