One line conversion of string to List<string> in C#

c#, list, string

Solution

Using collection initializer:

List<string> myList = new List<string>{myString};

Update. Or, as suggested in comments,

var myList = new List<string>{myString};

Problem

This is a rather simple, but annoying problem I am facing. There are many places in my application that can use either one or many strings... so to say. I have used a List of string by default. But every time I have a single string, I am doing this: ``` List<string> myList = new List<string>(); myList.Add(myString); ``` A simple `myString.ToList()` does not work since it converts into a list of characters and not a list with one string. Is there an efficient one line way to do this?

Original source