ईवेंट्स
17 मार्च, 9 pm - 21 मार्च, 10 am
साथी डेवलपर्स और विशेषज्ञों के साथ वास्तविक दुनिया के उपयोग के मामलों के आधार पर स्केलेबल एआई समाधान बनाने के लिए मीटअप श्रृंखला में शामिल हों।
अभी पंजीकरण करेंयह ब्राउज़र अब समर्थित नहीं है.
नवीनतम सुविधाओं, सुरक्षा अपडेट और तकनीकी सहायता का लाभ लेने के लिए Microsoft Edge में अपग्रेड करें.
Property | Value |
---|---|
Rule ID | IDE0300 |
Title | Use collection expression for array |
Category | Style |
Subcategory | Language rules (expression-level preferences) |
Applicable languages | C# 12+ |
Options | dotnet_style_prefer_collection_expression |
This rule flags places where a collection expression could be used to initialize an array. For example, this rule offers to simplify code like new C[] { ... }
, new[] { ... }
, and C[] c = { ... }
into the collection expression form ([...]
).
Options specify the behavior that you want the rule to enforce. For information about configuring options, see Option format.
Property | Value | Description |
---|---|---|
Option name | dotnet_style_prefer_collection_expression | |
Option values | true | when_types_exactly_match |
Prefer to use collection expressions only when types match exactly, for example, int[] i = new int[] { 1, 2, 3 }; . |
when_types_loosely_match (.NET 9 and later versions)* |
Prefer to use collection expressions even when types match loosely, for example, IEnumerable<int> i = new int[] { 1, 2, 3 }; . The targeted type must match the type on the right-hand side or be one of the following types: IEnumerable<T>, ICollection<T>, IList<T>, IReadOnlyCollection<T>, IReadOnlyList<T>. |
|
false | never |
Disables the rule. | |
Default option value | true in .NET 8when_types_loosely_match in .NET 9 and later versions |
*The code fix when this option is used might change the semantics of your code. For example, if you had IEnumerable<int> x = new int[] { 1, 2, 3 };
, then in the original code, an array is produced. But in the new code (IEnumerable<int> x = [1, 2, 3];
), an internal compiler-synthesized type is produced instead. You can observe this difference if you use an is
check or a cast.
// Code with violations.
int[] i = new int[] { 1, 2, 3 };
IEnumerable<int> j = new int[] { 1, 2, 3 };
// Fixed code.
int[] i = [1, 2, 3];
IEnumerable<int> j = [1, 2, 3];
If you want to suppress only a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.
#pragma warning disable IDE0300
// The code that's violating the rule is on this line.
#pragma warning restore IDE0300
To disable the rule for a file, folder, or project, set its severity to none
in the configuration file.
[*.{cs,vb}]
dotnet_diagnostic.IDE0300.severity = none
To disable all of the code-style rules, set the severity for the category Style
to none
in the configuration file.
[*.{cs,vb}]
dotnet_analyzer_diagnostic.category-Style.severity = none
For more information, see How to suppress code analysis warnings.
.NET प्रतिक्रिया
.NET एक ओपन सोर्स प्रोजेक्ट है. प्रतिक्रिया प्रदान करने के लिए लिंक का चयन करें:
ईवेंट्स
17 मार्च, 9 pm - 21 मार्च, 10 am
साथी डेवलपर्स और विशेषज्ञों के साथ वास्तविक दुनिया के उपयोग के मामलों के आधार पर स्केलेबल एआई समाधान बनाने के लिए मीटअप श्रृंखला में शामिल हों।
अभी पंजीकरण करें