mercredi 18 mars 2015

Is there any way to create an implicitly typed global variable in C#? [migrated]



if (i==101)
{
var lastLoopCheck;
}


Visual Studio 2013 displays an error:- "Implicitly-typed local variables must be initialized".


Which leads to the assumption that implicitly typed global variables can be left uninitialized, which further leads to the assumption that it is possible to create implicitly typed global variables.


But if I try creating a variable anywhere outside the static void Main() function, Visual Studio 2013 displays an error:- "A namespace cannot directly contain members such as fields or methods". So I did a google search and found this; the answer and the comments imply that technically, there is no such thing as a "global variable" in C# but you can create a public static variable inside a public static class, so the public static variable can be considered to be practically a global variable.


So now there are two possibilities-




  1. It is impossible to create an implicitly typed global variable in C#, but then why is the word "local" used in the first error. It just makes things confusing.




  2. I can't create it because of my lack of knowledge on how to do so (if this is the case then please tell me how to do it).




Which possibility is correct?





Aucun commentaire:

Enregistrer un commentaire