简体   繁体   English

在Asp.Net 2.0中使用什么代替“ var”

[英]What to use instead of “var” in Asp.Net 2.0

I'm craeting 2 dependent drop down menus. 我正在创建2个依赖的下拉菜单。 So I used the code in this link's solution: Dynamically add drop down lists and remember them through postbacks 因此,我在此链接的解决方案中使用了代码: 动态添加下拉列表并通过回发记住它们

But I guess the problem is that var usage belongs to 3.5. 但是我想问题是var用法属于3.5。 So Visual Studio doesn't recognize it. 因此,Visual Studio无法识别它。 So what can I use instead of var in this line? 那么,在这一行中我可以用什么代替var?

var items = new List<ListItem>();

只是使用要创建的对象的类型?

List<ListItem> items = new List<ListItem>();

The var keyword was introduced in C# 3.0. var关键字是在C#3.0中引入的。 It declares an implicitly typed variable where the compiler infers the type of the variable. 它声明一个隐式类型的变量,编译器在其中推断变量的类型。 It is a convenience but if you don't want to use it (or cannot use it in older versions of C#) you can declare the variable using an explicit type instead. 这很方便,但是如果您不想使用它(或者在较旧的C#版本中不能使用它),则可以使用显式类型声明变量。

In your case you have to do it like this: 在您的情况下,您必须这样做:

List<ListItem> items = new List<ListItem>();

You can read more about implicitly typed local variables on MSDN. 您可以在MSDN上阅读有关隐式类型局部变量的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM