Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between the implementation of var in Javascript and C#

I would like to ask a theoretical question. If I have, for example, the following C# code in Page_load:

cars = new carsModel.carsEntities();  var mftQuery = from mft in cars.Manufacturers                 where mft.StockHeaders.Any(sh=> sh.StockCount>0)                 orderby mft.CompanyName                 select new {mft.CompanyID, mft.CompanyName};                // ... 

Questions:

  1. This code uses the var keyword. What is the benefit of this construct?
  2. What is the key difference between the implementation of var in Javascript and C#?
like image 782
dali1985 Avatar asked Dec 10 '11 16:12

dali1985


People also ask

What is the difference between JavaScript and C?

JavaScript is interpreted and sometimes compiled at runtime with a just-in-time (JIT) compiler. C is statically typed. JavaScript is dynamically typed. C requires programmers to allocate and reclaim blocks of memory.

What is the difference between VAR and this in JavaScript?

var variable is private. this variable is public. @elclanrs—that comment is unhelpful because it is misleading. Variables can be declared as globals, this is related to execution context, it is not a variable in the usual sense, though it can be considered a local variable.

What is the difference between VAR and without VAR in JavaScript?

Variables declared outside a function become GLOBAL, and all scripts and functions on the web page can access it. Global variables are destroyed when you close the page. If you declare a variable, without using "var", the variable always becomes GLOBAL.

Why we should not use VAR in JavaScript?

This means that if a variable is defined in a loop or in an if statement it can be accessed outside the block and accidentally redefined leading to a buggy program. As a general rule, you should avoid using the var keyword.


1 Answers

JavaScript is a dynamically typed language, while c# is (usually) a statically typed language. As a result, comparisons like this will always be problematic. But:

JavaScript's var keyword is somewhat similar to C#'s dynamic keyword. Both create a variable whose type will not be known until runtime, and whose misuse will not be discovered until runtime. This is the way JavaScript always is, but this behavior is brand new to C# 4.

dynamic foo = new DateTime(); foo.bar();  //compiles fine but blows up at runtime. 

JavaScript has nothing to match C#'s var, since JavaScript is a dynamically typed language, and C#'s var, despite popular misconception, creates a variable whose type is known at compile time. C#'s var serves two purposes: to declare variables whose type is a pain to write out, and to create variables that are of an anonymous type, and therefore have no type that can be written out by the developer.

For an example of the first:

var conn = new System.Data.SqlClient.SqlConnection("...."); 

Anonymous type projections from Linq-to-Sql or Entity Framework are a good example of the second:

var results = context.People.Where(p => p.Name == "Adam")                             .Select(p => new { p.Name, p.Address }); 

Here results is of type IQueryable<SomeTypeTheCompilerCreatedOnTheFly>. No matter how much you might like to write out the actual type of results, instead of just writing var, there's no way to since you have no knowledge of the type that the compiler is creating under the covers for your anonymous type—hence the terminology: this type is anonymous

In both cases the type is known at compile time, and in both cases, subsequently saying either

conn = new DateTime(); 

or

results = new DateTime(); 

would result in a compiler error, since you're setting conn and results to a type that's not compatible with what they were declared as.

like image 186
Adam Rackis Avatar answered Sep 21 '22 15:09

Adam Rackis