[英]How do I convert this javascript line to c#?
I understand the first part of the line, but I'm not sure what the part after the || 我了解该行的第一部分,但是我不确定||之后的那一部分。 does. 确实。 Can anyone tell me how to convert this line to c#? 谁能告诉我如何将此行转换为C#?
var g = grid[iy * gw + ix] || [];
Original source is here. 原始资料在这里。 http://www.dhteumeuleu.com/aqualibrium/source http://www.dhteumeuleu.com/aqualibrium/source
It's the logical or, but basically equivalent to C#'s null-coalescing operator ??
这是逻辑上的,或基本上等同于C#的空合并运算符??
. 。 It resolves to the left, or the right if the left is false
. 它向左解析,如果左为false
,则向右解析。
The one caveat is that, because of Javascript's dynamic typing, the situation isn't as clear-cut as in C#. 一个警告是,由于Javascript的动态键入,这种情况不像C#那样明确。 The ||
||
forces the left-hand value to be converted to a bool, and there are various values that will end up being converted to false
. 强制将左侧的值转换为布尔值,并且最终会有各种值转换为false
。 For example, Javascript will evaluate all these as "other": 例如,Javascript将所有这些评估为“其他”:
* false || 'other';
* 0 || 'other';
* '' || 'other';
* undefined || 'other';
As commented || 如评论|| is equivalent to c#'s to the coalesce operator ??. 与合并运算符??的c#等效。 And [] is an empty array. 并且[]是一个空数组。
In this case grid
is an array of arrays of Particles. 在这种情况下, grid
是粒子阵列的阵列。 The C# equivalent code would be: C#等效代码为:
// Particle[][] grid = ...
Particle[] g = grid[iy * gw + ix] ?? new Particle[];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.