19

what is the difference between "static" and "const" when it comes to declare global variables;

namespace General
{
    public static class Globals
    {
        public const double GMinimum = 1e-1;

        public const double GMaximum = 1e+1;
    }
}

which one is better (considering that these variables wont be changing ever)

namespace General
{
    public static class Globals
    {
        public static double GMinimum1 = 1e-1;

        public static double GMaximum1 = 1e+1;
    }
}
Ibrahim Ozdemir
  • 613
  • 1
  • 5
  • 18
  • In the second one, I could accidentally go `General.Globals.GMaximum1 = 2` and it would screw up a lot of other places. Only the first one is safe for something that shouldn't change, unless you declare the statics as `readonly` as well. – Ron Beyer May 28 '15 at 20:21
  • More here http://stackoverflow.com/questions/2216239/what-is-the-difference-between-a-static-and-const-variable – Nitin Aggarwal May 28 '15 at 20:23

3 Answers3

25

const and readonly perform a similar function on data members, but they have a few important differences. A constant member is defined at compile time and cannot be changed at runtime. Constants are declared as a field, using the const keyword and must be initialized as they are declared.

The static modifier is used to declare a static member, this means that the member is no longer tied to a specific object. The value belongs to the class, additionally the member can be accessed without creating an instance of the class. Only one copy of static fields and events exists, and static methods and properties can only access static fields and static events

t3dodson
  • 3,949
  • 2
  • 29
  • 40
Benjamin RD
  • 11,516
  • 14
  • 87
  • 157
  • Does "is no longer tied to a specific object" mean several objects using the same static variable name can confuse the value? I'm just finding out that most of my static variables work, but some return what *I think* is the value from a different class. Being unreliable might be an important distinction between the two. – user1566694 Nov 27 '17 at 20:24
  • 1
    @user1566694 I will point you to the docs for the [static keyword](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/static). You should be accessing the static member using the class name to disambiguate `Foo.Bar` would be the static property on class `Foo` which can be accessed without an instance of `Foo`. If you don't disambiguate than you can get into a confusing situation. – t3dodson Nov 29 '17 at 02:39
14

const variables cannot be changed ever after compile time. They are good for things that are truly constant (i.e. pi)

static members are shared memory that is accessible by all instances of a particular class and more if access modifiers like public are used (these may feel like globals variables in languages like javascript). Static members behave like normal variables that can be reassigned whenever.

In your case if the numbers are guaranteed never to change then make them const. If they do change you would have to recompile the program with a new value.


Which one is better? if you use const then the literal values get baked into the assembly and provide a performance boost.

If the values ever need to change then the time taken to change the source and recompile quickly ruins this marginal performance increase.

t3dodson
  • 3,949
  • 2
  • 29
  • 40
12

const is a constant value, and cannot be changed. It is compiled into the assembly.

static means that it is a value not related to an instance, and it can be changed at run-time (since it isn't readonly).

So if the values are never changed, use consts.

Maarten
  • 22,527
  • 3
  • 47
  • 68