Skip to content

Commit b6749af

Browse files
committed
Use collection expressions
1 parent 485daaa commit b6749af

29 files changed

+42
-45
lines changed

YamlDotNet/Core/Constants.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ namespace YamlDotNet.Core
2929
public static class Constants
3030
{
3131
public static readonly TagDirective[] DefaultTagDirectives =
32-
{
32+
[
3333
new TagDirective("!", "!"),
3434
new TagDirective("!!", "tag:yaml.org,2002:")
35-
};
35+
];
3636

3737
public const int MajorVersion = 1;
3838
public const int MinorVersion = 3;

YamlDotNet/Core/Emitter.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public class Emitter : IEmitter
5656
private readonly Stack<EmitterState> states = new Stack<EmitterState>();
5757
private readonly Queue<ParsingEvent> events = new Queue<ParsingEvent>();
5858
private readonly Stack<int> indents = new Stack<int>();
59-
private readonly TagDirectiveCollection tagDirectives = new TagDirectiveCollection();
59+
private readonly TagDirectiveCollection tagDirectives = [];
6060
private int indent;
6161
private int flowLevel;
6262
private bool isMappingContext;

YamlDotNet/Core/MergingParser.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,9 @@ private sealed class ParsingEventCollection : IEnumerable<LinkedListNode<Parsing
173173

174174
public ParsingEventCollection()
175175
{
176-
events = new LinkedList<ParsingEvent>();
177-
deleted = new HashSet<LinkedListNode<ParsingEvent>>();
178-
references = new Dictionary<AnchorName, LinkedListNode<ParsingEvent>>();
176+
events = [];
177+
deleted = [];
178+
references = [];
179179
}
180180

181181
public void AddAfter(LinkedListNode<ParsingEvent> node, IEnumerable<ParsingEvent> items)

YamlDotNet/Core/Parser.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ namespace YamlDotNet.Core
3636
public class Parser : IParser
3737
{
3838
private readonly Stack<ParserState> states = new Stack<ParserState>();
39-
private readonly TagDirectiveCollection tagDirectives = new TagDirectiveCollection();
39+
private readonly TagDirectiveCollection tagDirectives = [];
4040
private ParserState state;
4141

4242
private readonly IScanner scanner;

YamlDotNet/Helpers/FsharpHelper.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public static bool IsFsharpListType(Type t)
7373
.GetType("Microsoft.FSharp.Collections.ListModule")
7474
.GetMethod("OfArray")
7575
.MakeGenericMethod(itemsType)
76-
.Invoke(null, new[] { arr });
76+
.Invoke(null, [arr]);
7777

7878
return fsharpList;
7979
}

YamlDotNet/Helpers/OrderedDictionary.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public OrderedDictionary() : this(EqualityComparer<TKey>.Default)
7676

7777
public OrderedDictionary(IEqualityComparer<TKey> comparer)
7878
{
79-
list = new List<KeyValuePair<TKey, TValue>>();
79+
list = [];
8080
dictionary = new Dictionary<TKey, TValue>(comparer);
8181
this.comparer = comparer;
8282
}
@@ -192,7 +192,7 @@ public bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value) =>
192192
internal void OnDeserializedMethod(StreamingContext context)
193193
{
194194
// Reconstruct the dictionary from the serialized list
195-
dictionary = new Dictionary<TKey, TValue>();
195+
dictionary = [];
196196
foreach (var kvp in list)
197197
{
198198
dictionary[kvp.Key] = kvp.Value;

YamlDotNet/RepresentationModel/DocumentLoadingState.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ namespace YamlDotNet.RepresentationModel
3131
/// </summary>
3232
internal class DocumentLoadingState
3333
{
34-
private readonly Dictionary<AnchorName, YamlNode> anchors = new Dictionary<AnchorName, YamlNode>();
35-
private readonly List<YamlNode> nodesWithUnresolvedAliases = new List<YamlNode>();
34+
private readonly Dictionary<AnchorName, YamlNode> anchors = [];
35+
private readonly List<YamlNode> nodesWithUnresolvedAliases = [];
3636

3737
/// <summary>
3838
/// Adds the specified node to the anchor list.

YamlDotNet/RepresentationModel/EmitterState.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ internal class EmitterState
3333
/// Gets the already emitted anchors.
3434
/// </summary>
3535
/// <value>The emitted anchors.</value>
36-
public HashSet<AnchorName> EmittedAnchors { get; } = new HashSet<AnchorName>();
36+
public HashSet<AnchorName> EmittedAnchors { get; } = [];
3737
}
3838
}

YamlDotNet/RepresentationModel/YamlDocument.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,11 @@ internal YamlDocument(IParser parser)
9090
/// </summary>
9191
private class AnchorAssigningVisitor : YamlVisitorBase
9292
{
93-
private readonly HashSet<AnchorName> existingAnchors = new HashSet<AnchorName>();
93+
private readonly HashSet<AnchorName> existingAnchors = [];
9494
/// <summary>
9595
/// Key: Node, Value: IsDuplicate
9696
/// </summary>
97-
private readonly Dictionary<YamlNode, bool> visitedNodes = new Dictionary<YamlNode, bool>();
97+
private readonly Dictionary<YamlNode, bool> visitedNodes = [];
9898

9999
public void AssignAnchors(YamlDocument document)
100100
{

YamlDotNet/RepresentationModel/YamlMappingNode.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ namespace YamlDotNet.RepresentationModel
3636
/// </summary>
3737
public sealed class YamlMappingNode : YamlNode, IEnumerable<KeyValuePair<YamlNode, YamlNode>>, IYamlConvertible
3838
{
39-
private readonly OrderedDictionary<YamlNode, YamlNode> children = new OrderedDictionary<YamlNode, YamlNode>();
39+
private readonly OrderedDictionary<YamlNode, YamlNode> children = [];
4040

4141
/// <summary>
4242
/// Gets the children of the current node.
@@ -196,13 +196,13 @@ internal override void ResolveAliases(DocumentLoadingState state)
196196
{
197197
if (entry.Key is YamlAliasNode)
198198
{
199-
keysToUpdate ??= new Dictionary<YamlNode, YamlNode>();
199+
keysToUpdate ??= [];
200200
// TODO: The representation model should be redesigned, because here the anchor could be null but that would be invalid YAML
201201
keysToUpdate.Add(entry.Key, state.GetNode(entry.Key.Anchor!, entry.Key.Start, entry.Key.End));
202202
}
203203
if (entry.Value is YamlAliasNode)
204204
{
205-
valuesToUpdate ??= new Dictionary<YamlNode, YamlNode>();
205+
valuesToUpdate ??= [];
206206
// TODO: The representation model should be redesigned, because here the anchor could be null but that would be invalid YAML
207207
valuesToUpdate.Add(entry.Key, state.GetNode(entry.Value.Anchor!, entry.Value.Start, entry.Value.End));
208208
}

YamlDotNet/RepresentationModel/YamlSequenceNode.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ namespace YamlDotNet.RepresentationModel
3636
[DebuggerDisplay("Count = {children.Count}")]
3737
public sealed class YamlSequenceNode : YamlNode, IEnumerable<YamlNode>, IYamlConvertible
3838
{
39-
private readonly List<YamlNode> children = new List<YamlNode>();
39+
private readonly List<YamlNode> children = [];
4040

4141
/// <summary>
4242
/// Gets the collection of child nodes.

YamlDotNet/Serialization/BufferedDeserialization/TypeDiscriminatingNodeDeserializerOptions.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ namespace YamlDotNet.Serialization.BufferedDeserialization
3030
{
3131
public class TypeDiscriminatingNodeDeserializerOptions : ITypeDiscriminatingNodeDeserializerOptions
3232
{
33-
internal readonly List<ITypeDiscriminator> discriminators = new List<ITypeDiscriminator>();
33+
internal readonly List<ITypeDiscriminator> discriminators = [];
3434

3535
/// <summary>
3636
/// Adds an <see cref="ITypeDiscriminator" /> to be checked by the TypeDiscriminatingNodeDeserializer.
@@ -64,4 +64,4 @@ public void AddUniqueKeyTypeDiscriminator<T>(IDictionary<string, Type> uniqueKey
6464
this.discriminators.Add(new UniqueKeyTypeDiscriminator(typeof(T), uniqueKeyTypeMapping));
6565
}
6666
}
67-
}
67+
}

YamlDotNet/Serialization/BuilderSkeleton.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ internal BuilderSkeleton(ITypeResolver typeResolver)
5858
{ typeof(SystemTypeConverter), _ => new SystemTypeConverter() }
5959
};
6060

61-
typeInspectorFactories = new LazyComponentRegistrationList<ITypeInspector, ITypeInspector>();
61+
typeInspectorFactories = [];
6262
this.typeResolver = typeResolver ?? throw new ArgumentNullException(nameof(typeResolver));
6363
settings = new Settings();
6464
}

YamlDotNet/Serialization/DeserializerBuilder.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public sealed class DeserializerBuilder : BuilderSkeleton<DeserializerBuilder>
7171
public DeserializerBuilder()
7272
: base(new StaticTypeResolver())
7373
{
74-
typeMappings = new Dictionary<Type, Type>();
74+
typeMappings = [];
7575
objectFactory = new Lazy<IObjectFactory>(() => new DefaultObjectFactory(typeMappings, settings), true);
7676

7777
tagMappings = new Dictionary<TagName, Type>

YamlDotNet/Serialization/LazyComponentRegistrationList.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ namespace YamlDotNet.Serialization
2828
{
2929
internal sealed class LazyComponentRegistrationList<TArgument, TComponent> : IEnumerable<Func<TArgument, TComponent>>
3030
{
31-
private readonly List<LazyComponentRegistration> entries = new List<LazyComponentRegistration>();
31+
private readonly List<LazyComponentRegistration> entries = [];
3232

3333
public LazyComponentRegistrationList<TArgument, TComponent> Clone()
3434
{

YamlDotNet/Serialization/ObjectGraphVisitors/AnchorAssigner.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ private class AnchorAssignment
3333
public AnchorName Anchor;
3434
}
3535

36-
private readonly Dictionary<object, AnchorAssignment> assignments = new Dictionary<object, AnchorAssignment>();
36+
private readonly Dictionary<object, AnchorAssignment> assignments = [];
3737
private uint nextId;
3838

3939
public AnchorAssigner(IEnumerable<IYamlTypeConverter> typeConverters)

YamlDotNet/Serialization/ObjectGraphVisitors/AnchorAssigningObjectGraphVisitor.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public sealed class AnchorAssigningObjectGraphVisitor : ChainedObjectGraphVisito
2929
{
3030
private readonly IEventEmitter eventEmitter;
3131
private readonly IAliasProvider aliasProvider;
32-
private readonly HashSet<AnchorName> emittedAliases = new HashSet<AnchorName>();
32+
private readonly HashSet<AnchorName> emittedAliases = [];
3333

3434
public AnchorAssigningObjectGraphVisitor(IObjectGraphVisitor<IEmitter> nextVisitor, IEventEmitter eventEmitter, IAliasProvider aliasProvider)
3535
: base(nextVisitor)

YamlDotNet/Serialization/SerializerBuilder.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public sealed class SerializerBuilder : BuilderSkeleton<SerializerBuilder>
5555
private readonly LazyComponentRegistrationList<IEnumerable<IYamlTypeConverter>, IObjectGraphVisitor<Nothing>> preProcessingPhaseObjectGraphVisitorFactories;
5656
private readonly LazyComponentRegistrationList<EmissionPhaseObjectGraphVisitorArgs, IObjectGraphVisitor<IEmitter>> emissionPhaseObjectGraphVisitorFactories;
5757
private readonly LazyComponentRegistrationList<IEventEmitter, IEventEmitter> eventEmitterFactories;
58-
private readonly Dictionary<Type, TagName> tagMappings = new Dictionary<Type, TagName>();
58+
private readonly Dictionary<Type, TagName> tagMappings = [];
5959
private readonly IObjectFactory objectFactory;
6060
private int maximumRecursion = 50;
6161
private EmitterSettings emitterSettings = EmitterSettings.Default;

YamlDotNet/Serialization/StaticBuilderSkeleton.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ internal StaticBuilderSkeleton(ITypeResolver typeResolver)
5050
{ typeof(GuidConverter), _ => new GuidConverter(false) },
5151
};
5252

53-
typeInspectorFactories = new LazyComponentRegistrationList<ITypeInspector, ITypeInspector>();
53+
typeInspectorFactories = [];
5454
this.typeResolver = typeResolver ?? throw new ArgumentNullException(nameof(typeResolver));
5555
settings = new Settings();
5656
}

YamlDotNet/Serialization/StaticDeserializerBuilder.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public StaticDeserializerBuilder(StaticContext context)
6969
{
7070
this.context = context;
7171
factory = context.GetFactory();
72-
typeMappings = new Dictionary<Type, Type>();
72+
typeMappings = [];
7373

7474
tagMappings = new Dictionary<TagName, Type>
7575
{

YamlDotNet/Serialization/StaticSerializerBuilder.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public sealed class StaticSerializerBuilder : StaticBuilderSkeleton<StaticSerial
5252
private readonly LazyComponentRegistrationList<IEnumerable<IYamlTypeConverter>, IObjectGraphVisitor<Nothing>> preProcessingPhaseObjectGraphVisitorFactories;
5353
private readonly LazyComponentRegistrationList<EmissionPhaseObjectGraphVisitorArgs, IObjectGraphVisitor<IEmitter>> emissionPhaseObjectGraphVisitorFactories;
5454
private readonly LazyComponentRegistrationList<IEventEmitter, IEventEmitter> eventEmitterFactories;
55-
private readonly Dictionary<Type, TagName> tagMappings = new Dictionary<Type, TagName>();
55+
private readonly Dictionary<Type, TagName> tagMappings = [];
5656
private int maximumRecursion = 50;
5757
private EmitterSettings emitterSettings = EmitterSettings.Default;
5858
private DefaultValuesHandling defaultValuesHandlingConfiguration = DefaultValuesHandling.Preserve;

YamlDotNet/Serialization/StreamFragment.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ namespace YamlDotNet.Serialization
3232
/// </summary>
3333
public sealed class StreamFragment : IYamlConvertible
3434
{
35-
private readonly List<ParsingEvent> events = new List<ParsingEvent>();
35+
private readonly List<ParsingEvent> events = [];
3636

3737
/// <summary>
3838
/// Gets or sets the events.

YamlDotNet/Serialization/TagMappings.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public sealed class TagMappings
3636
/// </summary>
3737
public TagMappings()
3838
{
39-
mappings = new Dictionary<string, Type>();
39+
mappings = [];
4040
}
4141

4242
/// <summary>

YamlDotNet/Serialization/Utilities/ObjectAnchorCollection.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ namespace YamlDotNet.Serialization.Utilities
2727
{
2828
internal sealed class ObjectAnchorCollection
2929
{
30-
private readonly Dictionary<string, object> objectsByAnchor = new Dictionary<string, object>();
31-
private readonly Dictionary<object, string> anchorsByObject = new Dictionary<object, string>();
30+
private readonly Dictionary<string, object> objectsByAnchor = [];
31+
private readonly Dictionary<object, string> anchorsByObject = [];
3232

3333
/// <summary>
3434
/// Adds the specified anchor.

YamlDotNet/Serialization/Utilities/SerializerState.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ namespace YamlDotNet.Serialization.Utilities
3131
/// </summary>
3232
public sealed class SerializerState : IDisposable
3333
{
34-
private readonly Dictionary<Type, object> items = new Dictionary<Type, object>();
34+
private readonly Dictionary<Type, object> items = [];
3535

3636
public T Get<T>()
3737
where T : class, new()

YamlDotNet/Serialization/Utilities/TypeConverter.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public static T ChangeType<T>(object? value, INamingConvention enumNamingConvent
180180
{
181181
try
182182
{
183-
return method.Invoke(null, new[] { value });
183+
return method.Invoke(null, [value]);
184184
}
185185
catch (TargetInvocationException ex)
186186
{
@@ -200,14 +200,14 @@ public static T ChangeType<T>(object? value, INamingConvention enumNamingConvent
200200
var parseMethod = destinationType.GetPublicStaticMethod("Parse", typeof(string), typeof(IFormatProvider));
201201
if (parseMethod != null)
202202
{
203-
return parseMethod.Invoke(null, new object[] { value, culture });
203+
return parseMethod.Invoke(null, [value, culture]);
204204
}
205205

206206
// Try with - public static T Parse(string)
207207
parseMethod = destinationType.GetPublicStaticMethod("Parse", typeof(string));
208208
if (parseMethod != null)
209209
{
210-
return parseMethod.Invoke(null, new object[] { value });
210+
return parseMethod.Invoke(null, [value]);
211211
}
212212
}
213213
catch (TargetInvocationException ex)

YamlDotNet/Serialization/YamlAttributeOverrides.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public int Matches(Type matchType)
108108
}
109109
}
110110

111-
private readonly Dictionary<AttributeKey, List<AttributeMapping>> overrides = new Dictionary<AttributeKey, List<AttributeMapping>>();
111+
private readonly Dictionary<AttributeKey, List<AttributeMapping>> overrides = [];
112112

113113
[return: MaybeNull]
114114
public T GetAttribute<T>(Type type, string member) where T : Attribute
@@ -150,7 +150,7 @@ public void Add(Type type, string member, Attribute attribute)
150150
var attributeKey = new AttributeKey(attribute.GetType(), member);
151151
if (!overrides.TryGetValue(attributeKey, out var mappings))
152152
{
153-
mappings = new List<AttributeMapping>();
153+
mappings = [];
154154
overrides.Add(attributeKey, mappings);
155155
}
156156
else if (mappings.Contains(mapping))

YamlDotNet/Serialization/YamlFormatter.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ public class YamlFormatter
3232
{
3333
CurrencyDecimalSeparator = ".",
3434
CurrencyGroupSeparator = "_",
35-
CurrencyGroupSizes = new[] { 3 },
35+
CurrencyGroupSizes = [3],
3636
CurrencySymbol = string.Empty,
3737
CurrencyDecimalDigits = 99,
3838
NumberDecimalSeparator = ".",
3939
NumberGroupSeparator = "_",
40-
NumberGroupSizes = new[] { 3 },
40+
NumberGroupSizes = [3],
4141
NumberDecimalDigits = 99,
4242
NaNSymbol = ".nan",
4343
PositiveInfinitySymbol = ".inf",

YamlDotNet/YamlDotNet.csproj

-3
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,14 @@
6161
<NoWarn>$(NoWarn);CA1725</NoWarn> <!-- Parameter names should match base declaration and other partial definitions -->
6262
<NoWarn>$(NoWarn);CS1061;CA1016</NoWarn> <!-- Mark assemblies with assembly version -->
6363

64-
<NoWarn>$(NoWarn);IDE0028</NoWarn> <!-- Collection initialization can be simplified -->
6564
<NoWarn>$(NoWarn);IDE0045</NoWarn> <!-- 'if' statement can be simplified -->
6665
<NoWarn>$(NoWarn);IDE0046</NoWarn> <!-- 'if' statement can be simplified -->
6766
<NoWarn>$(NoWarn);IDE0047</NoWarn> <!-- Parentheses can be removed -->
6867
<NoWarn>$(NoWarn);IDE0078</NoWarn> <!-- Use pattern matching -->
6968
<NoWarn>$(NoWarn);IDE0083</NoWarn> <!-- Use pattern matching -->
7069
<NoWarn>$(NoWarn);IDE0090</NoWarn> <!-- 'new' expression can be simplified -->
7170
<NoWarn>$(NoWarn);IDE0110</NoWarn> <!-- Discard can be removed -->
72-
<NoWarn>$(NoWarn);IDE0251</NoWarn> <!-- Member can be made 'readonly' -->
7371
<NoWarn>$(NoWarn);IDE0290</NoWarn> <!-- Use primary constructor -->
74-
<NoWarn>$(NoWarn);IDE0300</NoWarn> <!-- Collection initialization can be simplified -->
7572
</PropertyGroup>
7673

7774
</Project>

0 commit comments

Comments
 (0)