In .NET 8 (C# 12) and later versions, if you have the dotnet_style_prefer_collection_expression option set to true, the code fix in Visual Studio converts your collection initialization code to use a collection expression (List<int> list = [1, 2, 3];). In Visual Basic and in .NET 7 (C# 11) and earlier versions, the code fix converts your code to use a collection initializer (List<int> list = new List<int> { 1, 2, 3 };).
Lưu ý
If you use the code fix in Visual Studio, the change it offers might have different semantics in some cases. For example, int[] x = new int[] { } is replaced with int[] x = [];, which has slightly different semantics—the compiler uses a singleton for x instead of creating a new instance.
Options
Set the values of the associated options for this rule to specify whether or not collection initializers and collection expressions are preferred when initializing collections.
For more information about configuring options, see Option format.
Bạn có thể tìm thấy nguồn cho nội dung này trên GitHub, nơi bạn cũng có thể tạo và xem lại các vấn đề và yêu cầu kéo. Để biết thêm thông tin, hãy xem hướng dẫn dành cho người đóng góp của chúng tôi.
Ý kiến phản hồi về .NET
.NET là một dự án nguồn mở. Chọn liên kết để cung cấp ý kiến phản hồi:
Tham gia chuỗi buổi gặp gỡ để xây dựng các giải pháp AI có thể mở rộng dựa trên các trường hợp sử dụng trong thế giới thực với các nhà phát triển và chuyên gia đồng nghiệp.