简体   繁体   中英

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

I'm craeting 2 dependent drop down menus. 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. So Visual Studio doesn't recognize it. So what can I use instead of var in this line?

var items = new List<ListItem>();

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

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

The var keyword was introduced in 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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