Difference between the implementation of var in Javascript and C#

后端 未结 5 766
情深已故
情深已故 2020-12-29 20:33

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 =          


        
5条回答
  •  不思量自难忘°
    2020-12-29 21:05

    The only thing they have in common is that they are used to declare local variables.

    The C# equivalent of JS var is dynamic. But since most C# users prefer static typing it is usually only used in interop scenarios of some kind(with COM or a dynamically typed language).

    C# var still uses static typing, but with an inferred type. Since JS only supports dynamic typing, there is no equivalent.

    In this context explicit static typing isn't possible, since new {mft.CompanyID, mft.CompanyName} creates an anonymous type, so implicit static typing with var is used.

提交回复
热议问题