kubilay
kubilay

Reputation: 5330

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

Upvotes: 0

Views: 1810

Answers (2)

Martin Liversage
Martin Liversage

Reputation: 106816

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.

Upvotes: 4

BoltClock
BoltClock

Reputation: 723468

Just use the type of the object being created?

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

Upvotes: 5

Related Questions