結合型を使用しますが、その理由を説明します。値の計算では、一連の変数を変更するのではなく、値を返す必要があるためです。複数の変数を変更する必要がある場合、多数の変数を変更してもスケーリングしません。次のものが何千も必要だとします。
IEnumerable<Ray> rays = GetAThousandRays();
var intersections = from ray in rays
where Intersect(ray, out distance, out normal)
orderby distance ...
クエリを実行すると、同じ 2 つの変数が繰り返し変更されます。変異している値に基づいて注文しています。これは混乱です。物事を変更するクエリを作成しないでください。それは非常に紛らわしいです。
あなたが望むものは:
var intersections = from ray in rays
let intersection = Intersect(ray)
where intersection.Intersects
orderby intersection.Distance ...
突然変異なし; 一連の値を変数としてではなく、値として操作します。
また、そのブール値フラグを取り除き、値を不変の構造体にする傾向もあります。
// returns null if there is no intersection
Intersection? Intersect(Ray ray) { ... }
struct Intersection
{
public double Distance { get; private set; }
public Vector3 Normal { get; private set; }
public Intersection(double distance, Vector3 normal) : this()
{
this.Normal = normal;
this.Distance = distance;
}
}