Questions tagged [var]

var is a keyword in a number of programming languages.

In computer programming, a variable or scalar is a storage location paired with an associated symbolic name (an identifier), which contains some known or unknown quantity of information referred to as a value.

The variable statement declares a variable, optionally initializing it to a value.

In C# it is used to declare a variable of an implied type.

Beginning in Visual C# 3.0, variables that are declared at method scope can have an implicit type var. An implicitly typed local variable is strongly typed just as if you had declared the type yourself, but the compiler determines the type. The following two declarations of i are functionally equivalent:

 var i = 10; // implicitly typed
 int i = 10; //explicitly typed

References:

2462 questions
20
votes
3 answers

Using var outside of a method

I wanted to use the var keyword to declare a field in my class however var only seems to work inside methods. The code I have looks like: public static Dictionary CommandList = new Dictionary{}; and I wanted to…
Jamie Dixon
  • 53,019
  • 19
  • 125
  • 162
20
votes
2 answers

Calling Clojure functions using var-quote syntax

Occasionally when looking at other people's Clojure code, I see a function defined via defn and then called using the var-quote syntax, e.g.: user> (defn a [] 1) #'user/a user> (a) ; This is how you normally call a function 1 user> (#'a) ; This…
Alex
  • 13,811
  • 1
  • 37
  • 50
19
votes
8 answers

How can I check whether only one out of three variables is not empty?

I have three variables: $var1 $var2 $var3 I'm actually looking for the best way to check if only one of these three variables is not empty and the two others are empty. Is that possible to do this with one if only? If not, then what's the best…
cetipabo
  • 265
  • 2
  • 8
19
votes
1 answer

A javascript 'let' global variable is not a property of 'window' unlike a global 'var'

I used to check if a global var has been defined with: if (window['myvar']==null) ... or if (window.myvar==null) ... It works with var myvar Now that I am trying to switch to let, this does not work anymore. var…
Fai Ng
  • 760
  • 1
  • 6
  • 14
19
votes
4 answers

How much impact does use of 'var' have on performance of C# Compiler?

I find the var keyword greatly helps in reducing noise in my C# code, with little loss of readability; I'd say that I now use explicit typing only when the compiler forces me to. I know that using var does not change the runtime characteristics of…
Samuel Jack
  • 32,712
  • 16
  • 118
  • 155
19
votes
2 answers

Initializing a 'var' to null

Is there any difference in runtime performance between the following variable initializations? var x = null as object; var x = (object) null; object x = null;
sharpener
  • 1,383
  • 11
  • 22
18
votes
2 answers

When is it okay to use "var" in Scala?

I know that Scala has var (for mutable state) but pure functional programming discourages use of any mutable state and rather focuses on using val for everything. Coming from an imperative world it's hard to let go of mutable state. My question is…
Soumya Simanta
  • 11,523
  • 24
  • 106
  • 161
17
votes
2 answers

How do "var" and raw types come together?

I came across an answer that suggests to use var list = new ArrayList(); I was surprised to find a raw type here, and I am simply wondering: does var use the <> "automatically? ( in the meantime, the answer was changed to use , but I am…
GhostCat
  • 137,827
  • 25
  • 176
  • 248
17
votes
3 answers

Cannot assign void to an implicitly-typed local variable with var and foreach

I'm trying to list all buttons name from my form to list with code var v = new List() { this }.ForEach(x => { x.GetType().Name.Contains(typeof(Button).Name); }); and always get error Cannot assign void to an implicitly-typed local…
tonni
  • 1,225
  • 4
  • 19
  • 35
17
votes
2 answers

Initialize value of 'var' in C# to null

I want to assign a variable to an initial value of null, and assign its value in the next if-else block, but the compiler is giving an error, Implicitly-typed local variables must be initialized. How could I achieve this?
Nikhil Chavan
  • 1,685
  • 2
  • 20
  • 34
16
votes
1 answer

Java 11: Local-Variable Syntax for Lambda Parameters - applications

I am curious about Java-11 in general, but specifically JEP:323 which plans to add the var declaration to Lambda operation variables. The motivation behind this feature is discussed nicely here. Consider the following quote from the article: // #1 -…
Rann Lifshitz
  • 4,040
  • 4
  • 22
  • 42
16
votes
8 answers

var in class gives error

Possible Duplicate: Using var outside of a method class A { string X; } // Proper class A { var X; } // Improper (gives error) Why is it, that i cant have var type variable declare in Class and what can be done in order to achieve it OR what is…
Pratik
  • 11,534
  • 22
  • 69
  • 99
16
votes
7 answers

What happens with "var" variables inside a JavaScript Constructor?

example: function Foo() { this.bla = 1; var blabla = 10; blablabla = 100; this.getBlabla = function () { return blabla; // exposes blabla outside } } foo = new Foo(); original question: I know that bla will be assigned…
Daniel
  • 1,562
  • 3
  • 22
  • 34
15
votes
4 answers

How properly declare a variable in Swift?

I found quite interesting these different ways to declare a variable in Swift: // METHOD 1 var dogName: String = "Charlie" // METHOD 2 var dogName: String { return "Charlie" } // METHOD 3 let dogName = { return "Charlie" } // METHOD 4 var…
Massimo Polimeni
  • 4,826
  • 4
  • 27
  • 54
15
votes
7 answers

Is there an Objective-C equivalent to C#'s 'var' keyword?

Huge proponent of using the 'var' keyword in C# for cases where it's very clear. For instance, rather than this... ThisIsMyReallyLongFooClassName foo = new ThisIsMyReallyLongFooClassName(); I can type this... var foo = new…
Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286