0

I need a compact way to create a var object and initialize with constant strings

When I use the following

 var types = new List<string> {"blah1, blah2, blah3" };

I get only one element in my list. How can I initialize the list with 3 elements.

1
  • 1
    So if you give it one string, you get one string. If you want three, how many do you think you should give it? Commented Dec 6, 2013 at 16:25

2 Answers 2

2

If you want to separate the elements by , char, try using the Split method, for sample:

var list = "blah1, blah2, blah3".Split(',') //break by ,
                                .Select(x => x.Trim()) // remove spaces
                                .ToList(); // convert to list
Sign up to request clarification or add additional context in comments.

1 Comment

This works perfectly. Thanks very much for the prompt response.
2

Change

var types = new List<string> {"blah1, blah2, blah3" };

to

var types = new List<string> {"blah1", "blah2", "blah3" };

1 Comment

This works for me. Thanks a lot for the quick answer!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.