10

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?

Arthur Mamou-Mani
  • 2,303
  • 10
  • 43
  • 69
  • Thanks Henk, I didn't know that, why are there no `global variables` in c#, does that mean that the `field` concept is very very similar to the one of `global variables`? – Arthur Mamou-Mani Oct 16 '12 at 11:48

4 Answers4

6

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.

H H
  • 263,252
  • 30
  • 330
  • 514
  • Thanks Henk, I didn't know that, why are there no `global variables` in c#, does that mean that the `field` concept is very very similar to the one of `global variables`? – Arthur Mamou-Mani Oct 16 '12 at 11:48
  • 1
    The `static` concept is very close to 'global'. An instance field is very much non-global. And in any case, a property is just as (non-)global as a field with the same scope and modifiers. – H H Oct 16 '12 at 11:49
3

I think Wikipedia's definition is appropriate here:

In object-oriented programming, field (also called data member or member variable) is the data encapsulated within a class or object. In the case of a regular field (also called instance variable), for each instance of the object there is an instance variable: for example, an Employee class has a Name field and there is one distinct name per employee. A static field (also called class variable) is one variable, which is shared by all instances.

So a global variable is pretty much a static field (and therefore, a field).

Kevin Gosse
  • 38,392
  • 3
  • 78
  • 94
  • Thanks KooKiz, Henk Holterman says below that c# has no global variables. You are using the term here in the wider context of programming. Can the `static` keyword also be used for global variables or only for fields? – Arthur Mamou-Mani Oct 16 '12 at 12:07
  • 1
    @ArthurMamou-Mani To me, a global variable is a variable you can access in every scope. As such, C#'s static variables are global variables. – Kevin Gosse Oct 16 '12 at 12:33
1

Global variables are variables that is accessed in the entire scope as you say, usually this is done with static classes. Example code:

public class Demo {
    public static string ThisIsGlobal = "Global";
    private string _field = "this is a field in the class";
    public void DoSomething()
    {
        string localVariable = "Local";
        string localVariable2 = ThisIsGlobal; // VALID
    }

    public static void GlobalMethod()
    {
        string localVariable = _field; // THIS IS NOT VALID!
    }
}

Many people say that global variables and state are bad, I don't think so as long as you use it as it should be used. In the above example the ThisIsGlobal is a global variable because it have the static keyword. As you see in the example you can access static variables from instance methods, but not instance variables from static methods.

Tomas Jansson
  • 22,767
  • 13
  • 83
  • 137
  • Thank you very much Tomas, Henk Holterman says that c# has no `global variables` but you are using them here. Can the `static` keyword be used for `global variables` too? – Arthur Mamou-Mani Oct 16 '12 at 11:54
  • For it to be global it must also be `public`. Global variables is an abstract concept, it doesn't have to be a variable. If you are using a a singleton, that is also a "global variabel". Or if you make it more advanced and uses dependency injection, but only have one instance of one of the objects that makes that object a global variable. But in its simplest form in C# I would say `static` is most common, but you can't say that it is global because it is `static`. It can be a `private static` and that doesn't make it "global", in that case it is shared by all instances of that type. – Tomas Jansson Oct 16 '12 at 13:16
0

Lots of variables sit outside of a particular instance of a class but they are all still contained within "some" class. Basically a global variable sits nearer the top of the object graph high in the sky where it can be seen/referenced by all the later/ lower classes/members in the object graph.

But a global variable is still just a field of some class/module.

rism
  • 11,932
  • 16
  • 76
  • 116
  • Thanks rism, how high "in the sky" does a variable has to be to be global or is it a relative position? Is a `global variable` a field of something else then? Finally, can I visualize this `object graph`? – Arthur Mamou-Mani Oct 16 '12 at 11:57
  • 1
    Personally if someone was talking global I'd expect to find it very near the top of the object graph, if not on the object that is the Main entry point to the app or in the 'designated" area for such things, such as Global.asax in web apps i.e. something that is processed per request. Undoubtedly tho, as always I guess it "just depends", which I appreciate is never very satisfying. – rism Oct 19 '12 at 03:40