'Best way to create a new List with a simple string and List<string> as default values? [closed]
I'm looking to create a new List<string> in C# with 2 default values :
A simple string and a List<string>
My objective it's to create a new List who concat my string var and my List var
This is my current way and i'm looking for a better way :
public class TestClass
{
public string SimpleString { get; set; }
public List<string> ListString { get; set; }
public List<string> Example()
{
return new List<string> { SimpleString }.Concat(ListString).ToList();
}
}
Do you have a better way to achieve that ?
My dream it's to use like this : new List<string> { SimpleString, ListString }; but it's not possible
Regards
Solution 1:[1]
One thing you can do is work with IEnumerable<string> instead of List<string> It is a little more code, but this should perform much better than what you've been doing, at least in many common situations:
public IEnumerable<string> AsEnumerable()
{
yield return SimpleString;
foreach(string item in ListString) yield return item;
}
Then, if a consumer of the type really needs a list (hint: it's not as common as you might think. IEnumerable fills a LOT of those use cases and tends to perform better) they can append their own .ToList():
var stringList = MyObject.AsEnumerable().ToList();
But if I were going to do this, I might instead implement IEnumerable<string>:
public class TestClass : IEnumerable<string>
{
public string SimpleString { get; set; }
public List<string> ListString { get; set; }
public IEnumerator<string> GetEnumerator()
{
yield return SimpleString;
foreach(string item in ListString) yield return item;
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
Now I can use this directly in a foreach loop and I get the ToList() method for free from System.Linq. That is, all of the following work, without any additional code:
var stringList = MyObject.ToList();
var fooItems = MyObject.Where(s => s.Contains("foo"));
foreach(string s in MyObject) // ...
To me, this is worth it even if you hit a case where the IEnumerable option is slower, as long as it's not egregiously so.
Solution 2:[2]
Why do you want to do it in one line? This is pretty efficient.
public List<string> ToList()
{
var list = new List<string>();
list.Add(SimpleString);
list.AddRange(ListString);
return list;
}
Solution 3:[3]
You can use List<T> constructor accepting IEnumerable<T>:
var otherList = new List<string> { "foo" };
var list = new List<string>(otherList);
and then write your own version of Append() extension method supporting List<T>:
public static class ListExtensions
{
public static List<T> Append<T>(this List<T> source, T value)
{
source.Add(value);
return source;
}
}
so in the end you'll have:
var otherList = new List<string> { "foo" };
var list = new List<string>(otherList).Append("bar");
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Tanveer Badar |
| Solution 3 | Prolog |
