MVC学习笔记之MVC3中的自动类型判断

来源:互联网 发布:设计彩印淘宝推荐 编辑:程序博客网 时间:2024/06/05 03:19

Using Automatic Type Inference

      The C# var keyword allows you to define a local variable without explicitly specifying the variable type, as
demonstrated by Listing 5-23. This is called type inference, or implicit typing


var myVariable = new Product { Name = "Kayak", Category = "Watersports", Price = 275M };string name = myVariable.Name; // legalint count = myVariable.Count; // compiler error


It is not that myVariable doesn’t have a type. It is just that we are asking the compiler to infer it from the code.
You can see from the statements that follow that the compiler will allow only members of the inferred class—Product
in this case—to be called.

原创粉丝点击