C
C#•10mo ago
Mihane

Global Variables in new Visual Studio

Hello, I'm learning C# and I'm using the "new" Visual Studio setup (the one without Main and Program declaration). However how can I declare a global variable with this setup? I saw some videos and they put the global variables in class Program, however without those declaration, as far as I know, I'm writing inside Main method.
5 Replies
Pobiega
Pobiega•10mo ago
Well, there is no such thing as "global variables" in C#. You probably mean public static variables, which you can't do (on the Program class) when using top level statements So if you want that, revert to the old style. Thats a valid choice to do. Using top-level statements is optional
Mihane
MihaneOP•10mo ago
Thanks you opened a whole world for me..
Pobiega
Pobiega•10mo ago
Uh, no problems? 🙂
Mayor McCheese
Mayor McCheese•10mo ago
Just an opinion "global variables" are frequently a pragmatic compromise and not something you should pursue as common practice. Tread carefully
Lex Li
Lex Li•10mo ago
Global variables are usually a feature of the classic programming languages (C/C++), and modern OOP languages (Java/C#) do not like. They were common locations for bugs as you have no easy way to track their modifications. Instead, people have been suggested to use safer/OOP ways, and common patterns like Singletons.
Want results from more Discord servers?
Add your server