0

参照によって関数に無限の数のパラメーターを渡すことは可能ですか?

これが有効ではないことはわかっていますが、これを行う方法はありますか?

private bool Test(ref params object[] differentStructures)
{
    //Change array here and reflect changes per ref
}

TestStructOne test1 = default(TestStructOne);
TestStructTwo test2 = default(TestStructTwo);
TestStructOne test3 = default(TestStructOne);
if (Test(test1, test2, test3)) { //happy dance }

次のことができることはわかっていますが、すべてのオブジェクトを含むために余分な変数を作成する必要がなくなることを望んでいます...

private bool Test(ref object[] differentStructures)
{
    //Change array here and reflect changes per ref
}

TestStructOne test1 = default(TestStructOne);
TestStructTwo test2 = default(TestStructTwo);
TestStructOne test3 = default(TestStructOne);
object[] tests = new object[] { test1, test2, test3 };
if (Test(ref tests)) { //simi quazi happy dance }
4

1 に答える 1

1

したがって、単純な答えはノーです。メソッドに無限の数の参照を返すことはできません。

そのような機能の利点は何ですか?明らかに、どこから来たのか、誰がそれを使用しているのかに関係なく、任意のオブジェクトを変更できるメソッドが必要です。これは、クラスの単一責任原則を破るものではなく、それを神のオブジェクトにしています。

ただし、列挙内にインスタンスを作成することはできます。

private bool Test(object[] differentStructures)
{
    differentStructures[0] = someOtherRessource;
    differentStructures[1] = anotherDifferentRessource
    differentStructures[2] = thirdDifferentRessource
}

そして、次のように呼び出します。

var tests = new[] {
    default(TestStructOne),
    default(TestStructTwo),
    default(TestStructOne)
}
Test(tests);

これは次のことにつながります。

tests[0] = someOtherRessource;
tests[1] = anotherDifferentRessource
tests[2] = thirdDifferentRessource
于 2016-03-03T14:44:44.933 に答える