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,

Answers


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.


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

Try this:

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

ViewBag.AccountIdList = new SelectList(items);

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

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


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

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

Need Your Help

How to use KVO as an alternative to custom delegates

objective-c cocoa cocoa-bindings key-value-observing

I am an iOS programmer. I just started learning coding for MAC OSX . But now I am having tough time coding for MAC OSX . The terms KVO and Bindings making my life tougher. There are document avail...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.