Insatantiate Generic Collection with Initial Values?

J

Joe Cool

You know how you can instantiate an array of, say, strings and specify
intial values at the same time with something like:

string[] myStrings = new string[3] { "value1", "value2", "value3" };

Can the same type of thing be done for a List<string> object?

I've done a lot of web searching but I can't find any sample code that
illustrates this.
 
P

Peter Duniho

Joe said:
You know how you can instantiate an array of, say, strings and specify
intial values at the same time with something like:

string[] myStrings = new string[3] { "value1", "value2", "value3" };

Can the same type of thing be done for a List<string> object?

I've done a lot of web searching but I can't find any sample code that
illustrates this.

Not literally like that, no.

But, note that List<T> has a constructor overload that takes an
IEnumerable<T> as an argument. So you can do this:

List<string> myStrings = new List<string>(new string[] { "value1",
"value2", "value3" });

Pete
 
A

Adam Clauss

Peter said:
Not literally like that, no.

But, note that List<T> has a constructor overload that takes an
IEnumerable<T> as an argument. So you can do this:

List<string> myStrings = new List<string>(new string[] { "value1",
"value2", "value3" });

Pete

Is there a reason to not use:
List<string> myStrings = new List<string>
{
"value1",
"value2"
};

Seems to compile fine on VS2008 on a .NET 3.5 app.

-Adam
 
T

Tom Shelton

You know how you can instantiate an array of, say, strings and specify
intial values at the same time with something like:

string[] myStrings = new string[3] { "value1", "value2", "value3" };

Can the same type of thing be done for a List<string> object?

I've done a lot of web searching but I can't find any sample code that
illustrates this.

Yes - if your using vs2008 (C# 3.0):

List<string> strs = new List<string>() {"value1", "value2", "value3"};

you can do it with dictionary type classes as well:

Dictionary<int, string> dict = new Dictionary<int, string>() { {1, "Hi"}, {2, "Bye"}};

Look up collection initializers...
 
P

Peter Duniho

Adam said:
Peter said:
Not literally like that, no.

But, note that List<T> has a constructor overload that takes an
IEnumerable<T> as an argument. So you can do this:

List<string> myStrings = new List<string>(new string[] { "value1",
"value2", "value3" });

Pete

Is there a reason to not use:
List<string> myStrings = new List<string>
{
"value1",
"value2"
};

Seems to compile fine on VS2008 on a .NET 3.5 app.

No, I guess not. Believe it or not, I never realized that the
collection initializer syntax works with things other than arrays. In
hindsight, that was silly of me; there's a reason they called that
"collection initializer" and not "array initializer". :)

Either way, the same constructor gets called. The newer syntax seems
nicer to me though.

Pete
 
P

Peter Duniho

Peter said:
[...]
Either way, the same constructor gets called. The newer syntax seems
nicer to me though.

Oops. I take that back. The newer syntax may be nicer, but now that
I've spent a little time bringing myself up-to-date on the topic, I see
that the older syntax can be significantly more efficient.

The newer syntax doesn't use the constructor taking an IEnumerable<T>,
but instead generates a series of explicit calls to the Add() method for
the collection class. (Actually, this seems vaguely familiar to me…I
think I knew about this new feature at one point, and just forgot).

On the plus side, this is a more general-purpose approach that can be
applied to any collection type with an Add() method. But on the minus
side, it can result in a pretty bloated method and lots of extra
execution overhead. For a collection with just a small number of
elements, that may not matter much (and frankly, the initializer syntax
usually ought to be reserved for such situations), but for a large data
set, the more explicit approach can be much more efficient.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top