Line 45: |
Line 45: |
| crop.dead.Set(true); | | crop.dead.Set(true); |
| </source></li> | | </source></li> |
− | <li>Net types can't handle implicit casting on null net fields. Always check for null before comparing or implicitly casting to a value type: | + | <li>Quirk: when using [https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-conditional-operators null-conditional operators] with [https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/value-types value-type] net fields (like <tt>NetBool</tt> and <tt>NetInt</tt>), '''missing values will be equal to both null and <tt>default(T)</tt>''': |
| <source lang="c#"> | | <source lang="c#"> |
− | NPC villager = null;
| + | Item item = null; |
| + | if (item?.category == null && item?.category == 0) // both true |
| + | </source> |
| | | |
− | // crash: 'System.NullReferenceException' in Netcode.dll
| + | When comparing to the default value, check for null first to avoid that issue: |
− | if (villager?.name == "Abigail") | + | <source lang="c#"> |
| + | Item item = null; |
| + | if (item != null && item.category == 0) |
| + | </source> |
| | | |
− | // this is okay
| + | </li> |
− | if (villager != null && villager.name == "Abigail")
| |
− | </source></li>
| |
| </ul> | | </ul> |
| | | |