Keywords: C# | Collection Initializers | List<T> | Single-Element Lists | Code Conciseness
Abstract: This article explores concise syntax for instantiating List<T> with only one element in C#. By analyzing the use of collection initializers, it explains how to omit constructor parentheses and leverage implicit type conversion, providing code examples and performance considerations to help developers write cleaner and more efficient code.
Introduction
In C# programming, it is common to instantiate List<T> collections containing only a single element. Traditional methods, such as using array initializers or custom utility functions, are viable but often lead to verbose code that reduces readability. This article introduces a more concise inline syntax based on best practices.
Basic Usage of Collection Initializers
C# introduced collection initializers in version 3.0, allowing direct specification of initial elements when creating collection objects. For List<string>, the standard syntax is:
List<string> list = new List<string>() { "single value" };This syntax uses curly braces {} to enclose initial values, with the compiler automatically calling the Add method to insert elements.
Simplified Form by Omitting Constructor Parentheses
Further simplification can be achieved by omitting the parentheses after the constructor:
List<string> list = new List<string> { "single value" };When the type parameter can be inferred from context, the var keyword may also be used:
var list = new List<string> { "title" };This approach is not limited to single elements; it supports initialization with multiple elements as well:
List<string> list = new List<string> { "value1", "value2", "value3" };Underlying Mechanisms and Performance Analysis
Collection initializers are syntactic sugar that compile to equivalent code:
List<string> temp = new List<string>();
temp.Add("single value");
List<string> list = temp;For single-element lists, performance is comparable to directly calling the constructor with an array, but the code is more concise. Note that if creating many single-element lists frequently, object pooling optimizations should be considered.
Comparison with Alternative Methods
Compared to the original method new List<string>(new string[] { "title" }), collection initializers reduce nesting and type repetition. Custom utility functions like SingleItemList<T> provide a unified interface but add maintenance overhead, whereas the built-in syntax aligns better with language conventions.
Application Scenarios and Best Practices
This syntax is suitable for scenarios such as API return values or temporary collection creation. For example:
public IEnumerable<string> GetTitles() => new List<string> { "defaultTitle" };It is recommended to promote its use in team coding standards to enhance code consistency and readability.
Conclusion
By omitting constructor parentheses with collection initializers, C# offers a concise and efficient way to create single-element lists. Developers should master this feature to avoid unnecessary complexity and write more elegant applications.