String Array to List

S

shapper

Hello,

I am trying to create an object where one of the properties is a list
of string. So I am creating an array of strings and converting it to a
list. Something like:

Account a = new Account { Approved = true, Roles = (new String[]
{ "Admin", "Col" }).ToList());

I am talking about the Roles which is a List<String>

I get the error:
Syntax error, ',' expected

What am I missing?

Can't I create this object in one code line?

Thanks,
Miguel
 
S

shapper

Hello,

I am trying to create an object where one of the properties is a list
of string. So I am creating an array of strings and converting it to a
list. Something like:

Account a = new Account { Approved = true, Roles = (new String[]
{ "Admin", "Col" }).ToList());

I am talking about the Roles which is a List<String>

I get the error:
Syntax error, ',' expected

What am I missing?

Can't I create this object in one code line?

Thanks,
Miguel

Ooops, I solved it. I was missing the } to close the Account object.

Thanks,
Miguel
 
V

vanderghast

You meant:

var foo = new List<String>() { "blah", "blither" };

It seems that the constant list of string does not qualify as
IEnumerable<String>, so


var foo = new List<String>( { "blah", "blither" } );

fails to compile.



Vanderghast, Access MVP
 
G

Göran Andersson

vanderghast said:
You meant:

var foo = new List<String>() { "blah", "blither" };

It seems that the constant list of string does not qualify as
IEnumerable<String>, so


var foo = new List<String>( { "blah", "blither" } );

fails to compile.

Just remove the parentheses:

var foo = new List<String> { "blah", "blither" };
 
A

Arne Vajhøj

Göran Andersson said:
Just remove the parentheses:

var foo = new List<String> { "blah", "blither" };

var foo = new List<String>( new String[] { "blah", "blither" } );

should also compile.

But it is most relevant if still at 2.0.

Arne
 

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

Similar Threads

List<String> 15
Compare. Case 3
String Array 1
Sring[] to List<MyEnumeration> 5
Create List in Linq 4
List to CSV 3
ArrayList.ToArray to string array question 1
List conversion 2

Top