OrchardCMS/OrchardCore · error · ArgumentNullException
Parameters must have the same length
Error message
Parameters must have the same length
What it means
StringExtensions.Translate(from, to) builds a character mapping table and requires the 'from' and 'to' character arrays to be the same length so each source char has exactly one replacement. When lengths differ there is no valid pairing, so it throws ArgumentNullException with this message (using nameof(from) as the param name).
Solutions
- Make both arrays the same length, padding 'to' with a replacement (e.g. space or the original char) for extra 'from' entries
- Add a guard that trims 'from' to 'to'.Length or throws early with a clear message
- Move the transliteration table to a single data structure (e.g. pairs) that cannot desynchronize
Example fix
// before
var result = name.Translate(new[] { 'ä', 'ö', 'ü', 'ß' }, new[] { 'a', 'o', 'u' });
// after
var result = name.Translate(new[] { 'ä', 'ö', 'ü', 'ß' }, new[] { 'a', 'o', 'u', 's' }); Defensive patterns
Strategy: validation
Validate before calling
if (from.Length != to.Length)
throw new ArgumentException("Translate maps must have equal-length 'from' and 'to' arrays.");
var result = value.Translate(from, to); Try / catch
try
{
result = value.Translate(fromChars, toChars);
}
catch (ArgumentNullException ex) when (ex.Message.Contains("same length"))
{
_logger.LogError(ex, "Transliteration table lengths differ: {From} vs {To}", fromChars.Length, toChars.Length);
} Prevention
- Define transliteration tables as paired tuples so entries cannot desynchronize
- Add a startup or unit-test check that all from/to tables have equal length
- Whenever adding a source character, add its replacement in the same commit
When it happens
Trigger: Calling aString.Translate(fromChars, toChars) where from.Length != to.Length, e.g. Translate(new[]{'a','b'}, new[]{'x'})
Common situations: Hand-written transliteration tables where one source character was added but its replacement was forgotten; building the arrays dynamically from a config string split that produced uneven sides
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- File path must be a non-empty string.
- The values for and must not be the same.
- Cannot delete the root directory.
- The values for and must not be the same.
- MessageId can't be empty.
AI-assisted analysis of OrchardCMS/OrchardCore@4306c0717f (2026-09-13).
Data as JSON: /api/errors/920174389021ec0e.
Report an issue: GitHub.
Appendix: source
Thrown at src/OrchardCore/OrchardCore.ContentManagement.Abstractions/Utilities/StringExtensions.cs:300
}
return true;
}
public static string Translate(this string subject, char[] from, char[] to)
{
if (string.IsNullOrEmpty(subject))
{
return subject;
}
ArgumentNullException.ThrowIfNull(from);
ArgumentNullException.ThrowIfNull(to);
if (from.Length != to.Length)
{
throw new ArgumentNullException(nameof(from), "Parameters must have the same length");
}
var map = new Dictionary<char, char>(from.Length);
for (var i = 0; i < from.Length; i++)
{
map[from[i]] = to[i];
}
var result = new char[subject.Length];
for (var i = 0; i < subject.Length; i++)
{
var current = subject[i];
if (map.TryGetValue(current, out var value))
{
result[i] = value;
}
elseView on GitHub (pinned to 4306c0717f)