Scott Boettger
Scott Boettger

Reputation: 3667

What is the most efficient way to place a list of drives in a ComboBox using C#?

I am creating a program that allows the user to select a drive letter from a combo box. I am debating between populating the box using a list or an array. What is the best and most efficient way to do this?

Upvotes: 4

Views: 1068

Answers (4)

jsmith
jsmith

Reputation: 7278

Pretty simple:

ComboBox cb = new ComboBox();

string[] drives = Environment.GetLogicalDrives();

foreach (string drive in drives)
{
    cb.Items.add(drive);
}

Upvotes: 1

James
James

Reputation: 82136

I would do the following:

ListBox.Items.AddRange(Environment.GetLogicalDrives());

For the amount of drives you are going to have its hardly going to make a difference what way you do it.

Upvotes: 2

Henk Holterman
Henk Holterman

Reputation: 273581

'Efficient' is never going to be an issue here, with a max of 26 letters.

The combobox is going to copy to an internal list anyway, so as a source you can use whatever is most convenient.

Upvotes: 3

kemiller2002
kemiller2002

Reputation: 115528

For populating it, there is no discernible difference between a list and an array.

Personally, I would use a list as it is generally easier to use (can add/remove items, no fixed length etc.), and with generics, there is type safety just like an array. I know this has no difference in binding it to a list, but it makes it easier getting to that point.

Upvotes: 5

Related Questions