Shortcut for creating single item list in C#

前端 未结 13 1842
迷失自我
迷失自我 2020-12-14 05:06

In C#, is there an inline shortcut to instantiate a List with only one item.

I\'m currently doing:

new List( new string[] { \"         


        
相关标签:
13条回答
  • 2020-12-14 05:37

    You can also do

    new List<string>() { "string here" };
    
    0 讨论(0)
  • 2020-12-14 05:41

    Simply use this:

    List<string> list = new List<string>() { "single value" };
    

    You can even omit the () braces:

    List<string> list = new List<string> { "single value" };
    

    Update: of course this also works for more than one entry:

    List<string> list = new List<string> { "value1", "value2", ... };
    
    0 讨论(0)
  • 2020-12-14 05:42

    A different answer to my earlier one, based on exposure to the Google Java Collections:

    public static class Lists
    {
        public static List<T> Of<T>(T item)
        {
            return new List<T> { item };
        }
    }
    

    Then:

    List<string> x = Lists.Of("Hello");
    

    I advise checking out the GJC - it's got lots of interesting stuff in. (Personally I'd ignore the "alpha" tag - it's only the open source version which is "alpha" and it's based on a very stable and heavily used internal API.)

    0 讨论(0)
  • 2020-12-14 05:42

    Inspired by the other answers (and so I can pick it up whenever I need it!), but with naming/style aligned with F# (which has a standard singleton function per data structure*):

    namespace System.Collections.Generic
    {
        public static class List
        {
            public static List<T> Singleton<T>(T value) => new List<T>(1) { value };
        }
    }
    

    * except for ResizeArray itself of course, hence this question :)


    In practice I actually name it Create to align with other helpers I define such as Tuple.Create, Lazy.Create[2], LazyTask.Create etc:

    namespace System.Collections.Generic
    {
        public static class List
        {
            public static List<T> Create<T>(T value) => new List<T>(1) { value };
        }
    }
    

    [2]

    namespace System
    {
        public static class Lazy
        {
            public static Lazy<T> Create<T>(Func<T> factory) => new Lazy<T>(factory);
        }
    }
    
    0 讨论(0)
  • 2020-12-14 05:43
    new[] { "item" }.ToList();
    

    It's shorter than

    new List<string> { "item" };
    

    and you don't have to specify the type.

    0 讨论(0)
  • 2020-12-14 05:43

    Use an extension method with method chaining.

    public static List<T> WithItems(this List<T> list, params T[] items)
    {
        list.AddRange(items);
        return list;
    }
    

    This would let you do this:

    List<string> strings = new List<string>().WithItems("Yes");
    

    or

    List<string> strings = new List<string>().WithItems("Yes", "No", "Maybe So");
    

    Update

    You can now use list initializers:

    var strings = new List<string> { "This", "That", "The Other" };
    

    See http://msdn.microsoft.com/en-us/library/bb384062(v=vs.90).aspx

    0 讨论(0)
提交回复
热议问题