Skip to content

Commit

Permalink
Merge branch 'patch-1' of github.com:lechu445/CsvHelper into lechu445…
Browse files Browse the repository at this point in the history
…-patch-1
  • Loading branch information
JoshClose committed Oct 5, 2022
2 parents fef1e0b + b310c01 commit 4eea243
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 18 deletions.
13 changes: 3 additions & 10 deletions src/CsvHelper/Configuration/ClassMapCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public class ClassMapCollection
var currentType = type;
while (true)
{
if (data.ContainsKey(currentType))
if (data.TryGetValue(currentType, out var map))
{
return data[currentType];
return map;
}

currentType = currentType.GetTypeInfo().BaseType;
Expand Down Expand Up @@ -80,14 +80,7 @@ internal virtual void Add(ClassMap map)

var type = GetGenericCsvClassMapType(map.GetType()).GetGenericArguments().First();

if (data.ContainsKey(type))
{
data[type] = map;
}
else
{
data.Add(type, map);
}
data[type] = map;
}

/// <summary>
Expand Down
8 changes: 4 additions & 4 deletions src/CsvHelper/CsvReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1228,9 +1228,9 @@ public virtual int GetFieldIndex(IEnumerable<string> names, int index = 0, bool

// Caching the named index speeds up mappings that use ConvertUsing tremendously.
var nameKey = string.Join("_", names) + index;
if (namedIndexCache.ContainsKey(nameKey))
if (namedIndexCache.TryGetValue(nameKey, out var cache))
{
(var cachedName, var cachedIndex) = namedIndexCache[nameKey];
(var cachedName, var cachedIndex) = cache;
return namedIndexes[cachedName][cachedIndex];
}

Expand Down Expand Up @@ -1363,9 +1363,9 @@ protected virtual void ParseNamedIndexes()
{
var args = new PrepareHeaderForMatchArgs(headerRecord[i], i);
var name = prepareHeaderForMatch(args);
if (namedIndexes.ContainsKey(name))
if (namedIndexes.TryGetValue(name, out var index))
{
namedIndexes[name].Add(i);
index.Add(i);
}
else
{
Expand Down
8 changes: 4 additions & 4 deletions src/CsvHelper/TypeConversion/EnumConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ public EnumConverter(Type type)
var dict = ignoreCase
? enumNamesByAttributeNamesIgnoreCase
: enumNamesByAttributeNames;
if (dict.ContainsKey(text))
if (dict.TryGetValue(text, out var name))
{
return Enum.Parse(type, dict[text]);
return Enum.Parse(type, name);
}
}

Expand Down Expand Up @@ -110,9 +110,9 @@ public EnumConverter(Type type)
/// <inheritdoc/>
public override string? ConvertToString(object? value, IWriterRow row, MemberMapData memberMapData)
{
if (value != null && attributeNamesByEnumValues.ContainsKey(value))
if (value != null && attributeNamesByEnumValues.TryGetValue(value, out var name))
{
return attributeNamesByEnumValues[value];
return name;
}

return base.ConvertToString(value, row, memberMapData);
Expand Down

0 comments on commit 4eea243

Please sign in to comment.