user1321237
user1321237

Reputation:

Is there some way I could combine this C# code?

I have the following code. It looks to me like there is a way I could combine it into one statement but I am not sure how to do this.

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

var emptyItem = new SelectListItem(){
    Value = "",
    Text  = "00"
};

items.Add(emptyItem);

ViewBag.AccountIdList = new SelectList(items);

Can someone tell me if it's possible to simplify this.

Thanks,

Upvotes: 0

Views: 120

Answers (7)

IAbstract
IAbstract

Reputation: 19881

Less readable testable, IMO ...but you can write:

items.Add(new SelectedListItem(){
    Value = "",
    Text  = "00"
});  

I wouldn't recommend more than this in a single statement. This statement can also be refactored into a method accepting parameters for Value and Text:

// now this is a unit testable method
SelectedListItem CreateSelectedItem (string value, string text) {
   return new SelectedListItem(){
        Value = value,
        Text  = text
    };
}

Now you can write the following which is very clear in what it does while being concise:

ViewBag.AccountIdList = new SelectList(items.Add(CreateSelectedItem("someValue", "someText"));

Upvotes: 0

Ale Miralles
Ale Miralles

Reputation: 604

ViewBag.AccountIdList = new List<SelectListItem>{new SelectListItem{Value = "", Text  = "00"}};

Upvotes: 0

James Michael Hare
James Michael Hare

Reputation: 38397

Yes, you can use the collection and object initializers together to create the item, add it to the list, and wrap the list all in one statement.

ViewBag.AccountIdList = new SelectList(
    new List<SelectListItem>
    {
         new SelectListItem
         {
            Value = "",
            Text = "00"
         }
    });

The indentation style above is how I prefer to type it with all the curlies on their own line, but you could even one-line it if you wanted.

Either way it's a single statement.

And incidentally, since you are just passing the List<SelectListItem> to a SelectList constructor, which takes an IEnumerable, you could just pass an array of 1 instead of a list for a bit more performance:

ViewBag.AccountIdList = new SelectList(
    new []
    {
         new SelectListItem
         {
            Value = "",
            Text = "00"
         }
    });

Both would work the same in this case, the latter is a bit more efficient, but both are fine and it's up to you which you prefer. For more info I did a short blog entry comparing different ways to return a single item as an IEnumerable<T> sequence.

Upvotes: 8

Paula Bean
Paula Bean

Reputation: 458

ViewBag.AccountIdList = new SelectList(List items = new List { new SelectListItem{Value="",Text="00"}});

Upvotes: 0

Lukazoid
Lukazoid

Reputation: 19416

ViewBag.AccountIdList = new SelectList(new List<SelectListItem> { new SelectListItem { Value = "", Text = "00"} });

Upvotes: 2

contactmatt
contactmatt

Reputation: 18600

Try this:

var items = new List<SelectListItem>()
{
   new SelectListItem { Value = "", Text = "00" }
}

ViewBag.AccountIdList = new SelectList(items);

Upvotes: 1

Mark Rawlingson
Mark Rawlingson

Reputation: 148

Something like this would be the closest you could get it to.

List<SelectListItem> items = new List<SelectListItem>();
items.Add(new SelectListItem(){
    Value = "",
    Text  = "00"
});
ViewBag.AccountIdList = new SelectList(items);

Upvotes: 0

Related Questions