Alag
Alag

Reputation: 1408

Absolutely strange behavior in creating of instance

I will show you two screenshots and I am interested why it does ... what it does.

In the first screenshot you can see that I am able to create an instance of List<SelectListItem> named s, but one line after that, an exception is thrown and I am not able to create instance of same list named selectListItem.

On the second screenshot you can see that if I declare list above condition, I am able to create an instance of List<SelectListItem> named selectListItem on the same position.

Please, what it caused?

enter image description here enter image description here

Upvotes: 2

Views: 99

Answers (1)

lawliet29
lawliet29

Reputation: 355

I'd guess that something weird is happening when you reference selectListItem later in a closure.

You should be able to do something like this:

var selectListItem = ServiceProductModel
    .GetAll()
    .Select(spm => new SelectListItem {
        Value = spm.Id.ToString(),
        Text = string.Format(@"{0}", spm.Name)
    })
    .ToList();

Upvotes: 1

Related Questions