2009-01-20 45 views
93

在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"); 

是否有一個較短/更清潔的方式?

謝謝。

回答

168

只需使用這樣的:

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

7

在方法鏈中使用擴展方法。

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

1

你也可以做

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

使用擴展方法,邁克爾的想法導致一些更簡單:

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

1

我只想做

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

不同的回答我剛纔的一個,根據接觸到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)

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

非常相似,其他人發佈,但是它可以確保只針對單一分配空間項目最初。

當然,如果你知道你會在以後添加一堆東西,它可能不是一個好主意,但仍然值得一提。

0

對於在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; 
    } 
} 

但有使用該框架更簡單的方法?

2

我有這個小功能:

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語句。

(晚的答案,但我偶然發現了這個過時的歌曲,並認爲這可能是有益的)

1

由其他答案的啓發(與這樣我就可以把它撿起來,每當我需要它!),但與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); 
    } 
} 
1
new[] { "item" }.ToList(); 

這是短於

new List<string> { "item" }; 

,你不必指定類型。

0

嘗試VAR

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