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
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
Thanks you opened a whole world for me..
Uh, no problems? 🙂
Just an opinion "global variables" are frequently a pragmatic compromise and not something you should pursue as common practice. Tread carefully
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.