だから、ここに私の問題があります.IEnumerableである特定のオブジェクトがあり、そのコレクションには常に最大4つの要素があることが保証されています. さて、重要ではない理由から、エレガントな方法で、コレクションに 4 つの要素が含まれるように "強制" できるようにしたいと考えています。
私はすでにいくつかの調査を行っており、最も説得力のある候補は Zip ですが、最短のコレクションの終わりに到達すると圧縮を停止します。
独自の拡張メソッドを作成せずにこれを行う方法はありますか? 自分自身をよりよく説明するには:
var list1 = new List<Dog> {
new Dog { Name = "Puppy" }
}
var list2 = new List<Dog> {
new Dog { Name = "Puppy1" },
new Dog { Name = "Puppy2" },
new Dog { Name = "Puppy3" },
new Dog { Name = "Puppy4" },
}
var other1 = list1.ExtendToNElements(4).ToList();
//Now other1's first element is an instance of Dog with Name = "Puppy"
//And the following 3 elements are null, or have a Dog with no name
//I don't really care about that
var other2 = list2.ExtendToNElements(4).ToList();
//other2 is the same as list2, nothing got added.
前もって感謝します!