The difference between a `global variable` and a `field`?
Asked Answered
B

4

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?

Bookish answered 16/10, 2012 at 11:42 Comment(1)
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?Bookish
R
7

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.

Radiobroadcast answered 16/10, 2012 at 11:46 Comment(2)
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?Bookish
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.Radiobroadcast
E
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).

Eulaheulalee answered 16/10, 2012 at 11:48 Comment(2)
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?Bookish
@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.Eulaheulalee
C
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.

Culch answered 16/10, 2012 at 11:48 Comment(2)
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?Bookish
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.Culch
N
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.

Nicker answered 16/10, 2012 at 11:51 Comment(2)
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?Bookish
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.Nicker

© 2022 - 2024 — McMap. All rights reserved.