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#?
var g = grid[iy * gw + ix] || [];
Original source is here. http://www.dhteumeuleu.com/aqualibrium/source
It's the logical or, but basically equivalent to C#'s null-coalescing operator ??
. It resolves to the left, or the right if the left is false
.
The one caveat is that, because of Javascript's dynamic typing, the situation isn't as clear-cut as in 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
. For example, Javascript will evaluate all these as "other":
* false || 'other';
* 0 || 'other';
* '' || 'other';
* undefined || 'other';
As commented || is equivalent to c#'s to the coalesce operator ??. And [] is an empty array.
In this case grid
is an array of arrays of Particles. The C# equivalent code would be:
// Particle[][] grid = ...
Particle[] g = grid[iy * gw + ix] ?? new Particle[];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.