C
C#4mo ago
Skadwick

✅ 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?
No description
4 Replies
Jimmacle
Jimmacle4mo ago
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
Skadwick
Skadwick4mo ago
Perfect, thanks a ton 🙂
sibber
sibber4mo ago
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
MODiX
MODiX4mo ago
If you have no further questions, please use /close to mark the forum thread as answered
Want results from more Discord servers?
Add your server