Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the point of the var keyword? [duplicate]

Tags:

variables

c#

boo

The var keyword does away with the need for an explicit type declaration and I have read with interest the SO discussion of when it might be appropriate.

I have also read about (but not used) Boo which seems to take things a step further by making it optional to declare a local variable. With Boo, both the type and the declaration can be implied.

Which leads me to wonder, why did the C# language designers bother to include a var keyword at all?

Update: Yes, var supports Anonymous types, but anonymous types by themselves do not necessitate the var keyword...

var anon = new { Name = "Terry", Age = 34 }; 

versus

anon = new { Name = "Terry", Age = 34 }; 
like image 819
Ed Guiness Avatar asked Oct 16 '08 15:10

Ed Guiness


People also ask

What is the purpose of the var keyword?

The var keyword is used to declare variables in JavaScript. Before you use a variable in a JavaScript program, you must declare it. Variables are declared with the var keyword as follows. Storing a value in a variable is called variable initialization.

Is using var a good practice?

Using var means in most cases less typing than explicitly using types. Especially with longer, nested types like dictionaries and nested types, simply using var can save some keystrokes. Also the fact that when listing multiple variables, using var for all of them will result in a 'neater' looking piece of code.

Is it necessary to use var keyword while declaring variable?

Variables can be declared and initialize without the var keyword. However, a value must be assigned to a variable declared without the var keyword. The variables declared without the var keyword becomes global variables, irrespective of where they are declared.

What is the advantage of using var in C#?

The point of var is to allow anonymous types, without it they would not be possible and that is the reason it exists.


2 Answers

Without the var keyword it becomes possible to accidentally create a new variable when you had actually intended to use an already existing variable. e.g.

name = "fred";    ... Name = "barney"; // whoops! we meant to reuse name 
like image 163
Ferruccio Avatar answered Sep 22 '22 12:09

Ferruccio


Update: There are two related questions here, actually: 1. Why do I have to declare variables at all? 2. What use is "var" in a language that makes you declare variables?

The answers to (1) are numerous, and can be found elsewhere for this question. My answer to (2) is below:

As other commenters have said, LINQ uses this for its anonymous types. However, LINQ is actually an instance of a more general problem where the type of the right-hand side of an expression is either unknown to the programmer, or is extremely verbose. Consider:

SomeGeneric<VeryLongTypename<NestedTypename>> thing = new    SomeGeneric<VeryLongTypename<NestedTypename>>(); 

Verbose and error-prone, right? So now they let you do this:

var thing = new SomeGeneric<VeryLongTypename<NestedTypename>>(); 

By reducing the duplication of information, errors are eliminated. Note that there aren't just typing errors, here: it's possible for the type of the left-hand expression to be mistyped in such a way that the compiler can silently cast from left to right, but the cast actually loses some property of the rvalue. This is even more important when the types returned by the rvalue may be unknown or anonymous.

like image 30
JSBձոգչ Avatar answered Sep 20 '22 12:09

JSBձոգչ