VAR的JavaScript和C#的实现之间的区别 [英] Difference between the implementation of var in Javascript and C#

查看:152
本文介绍了VAR的JavaScript和C#的实现之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问一个理论问题。如果我有,例如,在Page_Load中下面的C#代码:

 汽车=新carsModel.carsEntities(); 

VAR mftQuery =从MFT中cars.Manufacturers
,其中mft.StockHeaders.Any(SH = GT; sh.StockCount大于0)
排序依据mft.CompanyName
选择新的{mft.CompanyID,mft.CompanyName};
// ...



问题:




  1. 此代码使用 VAR 关键字。这是什么结构的好处?

  2. 是什么 VAR JavaScript和C#的实现之间的主要区别?


解决方案

JavaScript是一种动态类型语言,而C#(通常)一个静态类型语言。其结果是,像这样的比较将总是有问题的。但是:



JavaScript的 VAR 关键字有点类似于C#的动态关键字。双方创建一个变量,其类型也不会知道,直到运行时,其滥用会不会被发现,直到运行时。这是JavaScript方式的总是的,但这种行为是全新的,以C#4。

 动态富=新的DateTime(); 
foo.bar(); //编译罚款,但在运行时炸毁。



JavaScript有没有匹配C#的 VAR ,由于JavaScript是一种动态类型语言,C#的 VAR ,尽管普遍的误解,创建一个变量,其类型为是在编译时已知。 C#的 VAR 有两个目的:声明变量,其类型是一个痛苦写出来,并创造出了既匿名类型的变量,因此带有没有类型,可以由开发者写出



有关的第一个例子:

  VAR康恩=新System.Data.SqlClient.SqlConnection(......); 



从LINQ到SQL或实体框架匿名类型的预测是第二的一个很好的例子:

  VAR的结果= context.People.Where(p => p.Name ==亚当)
。选择(p =>新建{p.Name,p.Address});

下面结果的类型为的IQueryable< SomeTypeTheCompilerCreatedOnTheFly> 。无论你可能想多少写出来的实际类型的结果,而不是只写 VAR ,也没有办法,因为你没有类型的知识,编译器在幕后正在为您的匿名类型,因此术语:这种类型的匿名



在这两种情况下,类型是在编译时间,然后在这两种情况下,随后说无论是

 康恩=新的DateTime(); 

  =结果新的datetime(); 



将导致的编译器错误,因为你设置康恩结果来一个类型,这不是跟他们有什么兼容的声明为


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#?

解决方案

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.

这篇关于VAR的JavaScript和C#的实现之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆