链接在C#和放大器性能;意外的结果放大器、意外、性能、链接

2023-09-06 09:04:04 作者:宁可过错也不错过

我只是有一个快速的读通过本文(具体为什么他选择使用结构/场,而不是类/属性中的位),看到这一行:

I was just having a quick read through this article (specifically the bit about why he chose to use structs / fields instead of classes / properties) and saw this line:

属性的结果是不是真正的L值,所以我们不能做这样的事情Vertex.Normal.dx = 0属性的链接提供了非常意外的结果。

The result of a property is not a true l-value so we cannot do something like Vertex.Normal.dx = 0. The chaining of properties gives very unexpected results.

什么样的​​意外结果是,他在说什么?

What sort of unexpected results is he talking about?

推荐答案

我想补充到dbemerlin的回答是,这里的关键是波多黎各的注意,属性的是不是左值,或者像我们所说的他们在C#中,变量。

I would add to dbemerlin's answer that the key here is Rico's note that properties are not "lvalues", or, as we call them in C#, "variables".

为了变异一个可变的结构(理想情况下,你不应该;可变的结构往往会导致更多的问题比解决的),你需要一个变异的变量的。这就是一个变量的是的 - 一个存储位置,其内容的变化。如果你有型载体的一个领域,你说

In order to mutate a mutable struct (and ideally, you should not; mutable structs often cause more problems than they solve) you need to mutate a variable. That's what a variable is -- a storage location whose contents change. If you have a field of type vector and you say

Foo.vector.x = 123;

那么我们的价值类型的变量 - 现场Foo.vector - 因此,我们可以发生变异,它的属性x。但如果你有值类型的属性:

then we have a variable of value type -- the field Foo.vector -- and we can therefore mutate its property x. But if you have a property of value type:

Foo.Vector.x = 123;

在属性的是不是一个变量。这相当于

the property is not a variable. This is equivalent to

Vector v = Foo.Vector;
v.x = 123;

该变异临时变量v,没有任何存储位置支持该属性。

which mutates the temporary variable v, not whatever storage location is backing the property.

整个问题消失,如果你放弃了可变的值类型。要改变X,使新值的新载体,更换整个事情:

The whole problem goes away if you abandon mutable value types. To change x, make a new vector with the new values and replace the whole thing:

Foo.Vector = new Vector(x, Foo.Vector.y);
 
精彩推荐