0

有没有一种简单的方法,最重要的是,更优化的代码:

class Chair{
    int numberOfLegs=4;
}

class House{
    Chair chair;
    String name="My Home";
}

// add chairs to each house
IList<Chair> chairs = new List<Chair>(10); // let us imagine that we have 10 different chairs...

// Code to replace:
IList<House> houses = new List<House>(chairs.Count());
for (int i = 0; i < houses.Count(); i++){
    houses[i].chair = chairs[i]
}
4

1 回答 1

5

我自己的建议是forLinq

// Code to replace:
IList<Homes> homes = chairs.Select(c => new Home{ Chair = c}).ToList();

它比旧代码更快吗?

你有更好的主意吗?也许使用chairs.AsParallel

于 2013-10-30T12:13:21.690 回答