fivem icon indicating copy to clipboard operation
fivem copied to clipboard

tweak(mono-rt2): Use null coalescing for Write & WriteLine

Open niekschoemaker opened this issue 1 year ago • 4 comments

Goal of this PR

This fixes an error if a null value is accidently passed into Debug.WriteLine

How is this PR achieving the goal

Using simple Null coalescing on the ToString call

This PR applies to the following area(s)

ScRT: C# v2

Successfully tested on

Tested the code seperate from the game, it prints nothing instead of causing a NullReferenceException.

Checklist

  • [x] Code compiles and has been tested successfully.
  • [x] Code explains itself well and/or is documented.
  • [x] My commit message explains what the changes do and what they are for.
  • [x] No extra compilation warnings are added by these changes.

niekschoemaker avatar Oct 19 '24 13:10 niekschoemaker

This is a somewhat opiniated change, so not sure which behavior is more desirable, printing nothing or Debug.WriteLine causing an Exception.

niekschoemaker avatar Oct 19 '24 13:10 niekschoemaker

Wouldn't it be better to print "null" instead of an empty string like in unity?

DaniGP17 avatar Oct 20 '24 12:10 DaniGP17

Wouldn't it be better to print "null" instead of an empty string like in unity?

Was thinking the same but would add some extra logic, and this reflects the default behavior of C#, Console.WriteLine(null) in C# prints nothing.

niekschoemaker avatar Oct 20 '24 13:10 niekschoemaker

Ohh okay, I've been checking how it was done in V1 of C# and it doesn't have this problem because the function expects a string instead of an object, so if you want to print it yourself you have to do the .ToString() and therefore The error is in your code and not in the Debug.WriteLine function itself

DaniGP17 avatar Oct 20 '24 14:10 DaniGP17