6

I have noticed there are other threads on global variables in c#. Such as integers, strings etc. e.g.

public static int;

But I need to use a "var" which the other thread don't mention and

public static var;

doesn't seem to work.

So what I'm asking is it possible to have a "var" as a global variable in c#?

difurious
  • 1,523
  • 3
  • 19
  • 32
  • Possible duplicate of: http://stackoverflow.com/questions/4043302/possible-to-make-a-global-var-c-sharp – itsme86 Jan 28 '13 at 22:49
  • are you familiar with static read-only variables or properties or protected static declaration..? I feel a `CLOSE` coming on here with this question – MethodMan Jan 28 '13 at 22:49
  • 1
    var is a C# keyword, can't use it as a name. Global variables = bad idea – Sten Petrov Jan 28 '13 at 22:49
  • 3
    `var` is *not* a type. The type of the variable is inferred at compile time from the declaration, that's why you have to immediately assign a value to it and can't use it to declare class members. – Manu Clementz Jan 28 '13 at 22:49

2 Answers2

6

No, because var is not a type itself, it merely takes on the form of whatever expression is on the righthand side of the assignment:

var num = 1;

is the same as:

int num = 1;

when declaring variables that are scoped outside of a method, you need to use the full type designator:

public static int num = 1;

or

public static int Num {get;set;}

etc

mellodev
  • 1,597
  • 12
  • 20
  • "When declaring variables that are scoped outside of a method, you need to use the full type designator"...Can you explain why so??? – Rasmita Dash Dec 04 '14 at 10:03
6

C# specification (section 26.1) reads:

[`var is] an implicitly typed local variable declaration ...

It goes further:

A local variable declarator in an implicitly typed local variable declaration is subject to the following restrictions:

  • The declarator must include an initializer.
  • The initializer must be an expression.
  • The initializer expression must have a compile-time type which cannot be the null type.
  • The local variable declaration cannot include multiple declarators.
  • The initializer cannot refer to the declared variable itself

So no, you cannot do this. Moreover I would recommend steering away from even thinking about global variables.

Global variables are not supported by the language. You may find alternatives in public static fields, but this will leak object state and break encapsulation.

Community
  • 1
  • 1
oleksii
  • 35,458
  • 16
  • 93
  • 163
  • Can you please give us a further reading on why to steer away from global variables inside of a class? I'm a developer with 4 years experience and I always use it knowing I shouldn't but I can't think of something different most of the time. Time to change the habits and the theory starting from the root I guess. – Baz Guvenkaya Jun 10 '15 at 06:07
  • 1
    @BarryGuvenkaya It depends on what you understand under global variable. In a context of this question, I think OP wanted to use `var` keyword for his variable. When I said global I meant *public member of a class*. Following encapsulation principle, members of a class should be private so no to leak implementation details and state of a class. If you want to use class as a data transfer object or you simply want to get its current state you can always use *public properties*. Does it make sense? – oleksii Jun 10 '15 at 09:22
  • That clears up sir thank you. Happy coding! – Baz Guvenkaya Jun 11 '15 at 00:23