✅ Best practices for initialization of variables at the start of a class, outside of methods?
Returning to C# after some time working almost exclusively with Lua, and I think I've confused myself. I was a bit surprised that I couldn't do work on this variable after it was declared - unless I do the work within the constructor or another method.
I'm starting to remember now, but I want to double check. Is the best practice here is to only do basic declaration at the start of the class, then any initialization or manipulation being done with the variable should be executed within the constructor, or a method?
4 Replies
initialization should be done in the constructor, as you can see by the errors you can't put loose statements inside a class
if the initialization is a one-liner and doesn't require instance data then you could also do it inline with the member declaration
Perfect, thanks a ton 🙂
if youre only initializing you dont need to do that in a constructor
huh discord suddenly loaded the rest of the messages
thats weird
i didnt see any reply
anyway
$close
If you have no further questions, please use /close to mark the forum thread as answered