Search code examples
c#variablesfieldconcept

The difference between a `global variable` and a `field`?


Fields are variables within a class or struct, local variables sit within a method and global variables are accessible in every scope (class and method included).

This makes me think that fields might be global variables but that global variables are not necessarily fields although I cannot think of a variable sitting outside of a class.

Is there a clear difference between the two?


Solution

  • You tagged this C# and C# does not really have 'global variables'.

    But a public static field (or property) would come close. The static makes it singular and gives it a 'global' lifetime.