在C中创建单个项目列表的快捷方式#

问题描述:

在C#中,是否有一个内联快捷方式来实例化列表<T>只有一个项目。在C中创建单个项目列表的快捷方式#

我目前做:

new List<string>(new string[] { "title" })) 

有了这个代码到处降低可读性。我想用这样的工具方法:

public static List<T> SingleItemList<T>(T value) 
{ 
    return (new List<T>(new T[] { value })); 
} 

所以,我可以这样做:

SingleItemList("title"); 

是否有一个较短/更清洁的方式?

谢谢。

只需使用这样的:

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

你甚至可以省略()括号:

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

更新:当然,这也适用于多个条目:

List<string> list = new List<string> { "value1", "value2", ... }; 
+4

在第一个选项的括号中放置1个文字,以便分配恰好一个空间的存储空间而不是默认值10 – 2009-01-20 20:14:22

+4

默认最终为4,我相信:新列表 {“Hello”} .Capacity == 4 – 2009-01-20 20:26:27

在方法链中使用扩展方法。

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

这将让你这样做:

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

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

更新

您现在可以使用列表初始化:

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

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

+0

也许这是由于自2009年以来的C#新版本,但我觉得'新目录 {”是的“}”会更好......? – ErikE 2014-07-18 00:02:37

+0

@ErikE根据Microsoft文档(http://msdn.microsoft.com/en-us/library/bb384062(v=vs.90).aspx),这在Visual Studio 2008中得到了支持。我不记得那么远了如果我在这个时候过分复杂的话,肯定会说。更新。 – 2014-12-11 18:30:56

你也可以做

new List<string>() { "string here" }; 

使用扩展方法,迈克尔的想法导致一些更简单:

public static List<T> InList(this T item) 
{ 
    return new List<T> { item }; 
} 

所以,你可以这样做:

List<string> foo = "Hello".InList(); 

我不知道是否我喜欢与否,介意你......

+0

我也不确定我喜欢它:这不是一个字符串类型(例如)的“奇怪”扩展吗? – M4N 2009-01-20 19:56:10

+1

我还没有把它扩展到书中的扩展方法,乔恩:-) 这似乎有点奇怪,但我喜欢它的实用性。 感谢Martin和Jon。 – 2009-01-20 20:08:33

我只想做

var list = new List<string> { "hello" }; 

不同的回答我刚才的一个,根据接触到Google Java Collections

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

然后:

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

我建议检查出GJC - 它有很多有趣的东西。(就我个人而言,我会忽略“alpha”标签 - 它只是t他的开源版本,这是“阿尔法”,它是基于一个非常稳定的,大量使用的内部API)

var list = new List<string>(1) { "hello" }; 

非常相似,其他人发布,但是它可以确保只针对单一分配空间项目最初。

当然,如果你知道你会在以后添加一堆东西,它可能不是一个好主意,但仍然值得一提。

对于在java中可枚举的单个项目,它将是Collections.singleton(“string”);

在C#这将是比新目录更加高效:

public class SingleEnumerator<T> : IEnumerable<T> 
{ 
    private readonly T m_Value; 

    public SingleEnumerator(T value) 
    { 
     m_Value = value; 
    } 

    public IEnumerator<T> GetEnumerator() 
    { 
     yield return m_Value; 
    } 

    IEnumerator IEnumerable.GetEnumerator() 
    { 
     yield return m_Value; 
    } 
} 

但有使用该框架更简单的方法?

我有这个小功能:

public static class CoreUtil 
{  
    public static IEnumerable<T> ToEnumerable<T>(params T[] items) 
    { 
     return items; 
    } 
} 

由于没有规定具体的返回类型,这是很通用,我用它所有的地方。您的代码看起来像

CoreUtil.ToEnumerable("title").ToList(); 

但是,当然,这也让

CoreUtil.ToEnumerable("title1", "title2", "title3").ToArray(); 

我经常用它时,我有附加/添加一个项目到LINQ语句的输出。比如在一个空白项添加到选择列表:

CoreUtil.ToEnumerable("").Concat(context.TrialTypes.Select(t => t.Name)) 

保存几ToList()Add语句。

(晚的答案,但我偶然发现了这个过时的歌曲,并认为这可能是有益的)

由其他答案的启发(与这样我就可以把它捡起来,每当我需要它!),但与F#(其具有每个数据结构*标准singleton功能)对齐命名/样式:

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

*除了ResizeArray本身当然,因此这个问题:)


在实践中我实际上命名Create与I定义其他助手对齐如Tuple.CreateLazy.Create [2],LazyTask.Create等:

namespace System.Collections.Generic 
{ 
    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); 
    } 
} 

new[] { "item" }.ToList(); 

这是短于

new List<string> { "item" }; 

,你不必指定类型。

的另一种方式,在C#/.Net Little wonders发现:

Enumerable.Repeat("value",1).ToList() 

尝试VAR

var s = new List<string> { "a", "bk", "ca", "d" };