开发者

What to use instead of "var" in Asp.Net 2.0

开发者 https://www.devze.com 2023-02-24 07:58 出处:网络
I\'m 开发者_开发技巧craeting 2 dependent drop down menus. So I used the code in this link\'s solution:

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>();


Just use the type of the object being created?

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号