dotnet/wpf · error
SR.IntegerCollectionLengthLessThanZero
Error message
SR.IntegerCollectionLengthLessThanZero
What it means
During binary XAML deserialization (BAML) of an IntegerCollection, DeserializeFrom reads an Int32 length prefix from the stream. If the length is negative the serialized data is corrupt or was produced by an incompatible writer, so ArgumentException(SR.IntegerCollectionLengthLessThanZero) is thrown before allocating the Int32Collection.
Solutions
- Rebuild the application so BAML resources are regenerated from valid XAML source.
- Verify the binary stream being passed to ConvertCustomBinaryToObject is intact and originates from a matching WPF compiler version.
- Regenerate the resource containing the Int32Collection (e.g. recompile the .xaml into the assembly).
Example fix
// before: deserializing a possibly corrupted stream
var obj = XamlInt32CollectionSerializer.StaticConvertCustomBinaryToObject(syncVersion, suspectBytes);
// after: guard length prefix first
if (BitConverter.ToInt32(suspectBytes, 0) < 0) throw new InvalidDataException("Corrupt IntegerCollection binary data");
var obj = XamlInt32CollectionSerializer.StaticConvertCustomBinaryToObject(syncVersion, suspectBytes); Defensive patterns
Strategy: validation
Validate before calling
if (data == null || data.Length < 4 || BitConverter.ToInt32(data, 0) < 0)
throw new InvalidDataException("IntegerCollection binary payload has invalid length prefix"); Try / catch
try { obj = serializer.ConvertCustomBinaryToObject(data); }
catch (ArgumentException ex) when (ex.Message.Contains("less than zero")) { /* treat as corrupt payload */ } Prevention
- Never hand-edit compiled BAML; always regenerate from XAML source.
- Validate binary resource integrity (size/hash) before deserialization.
When it happens
Trigger: Calling ConvertCustomBinaryToObject or StaticConvertCustomBinaryToObject on a BAML stream whose IntegerCollection record begins with a negative Int32 count (e.g. truncated, corrupted, or hand-crafted binary data).
Common situations: Corrupted or truncated compiled BAML resources in an assembly, binary data edited or generated by third-party tooling, or version mismatches where the binary XAML format changed between WPF builds.
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
- SR.UnknownIndexType
- Unrecognized float type in BAML file.
- ArgumentOutOfRangeException (timeout was Duration.Automatic)
- Can't Assign to Known Type attributes
- Collection_BadRank
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/773d0d68338b8128.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XamlInt32CollectionSerializer.cs:275
BinaryReader reader)
{
return DeserializeFrom( reader );
}
private static Int32Collection DeserializeFrom( BinaryReader reader )
{
Int32Collection theCollection;
IntegerCollectionType type;
type = (IntegerCollectionType) reader.ReadByte();
int count = reader.ReadInt32();
if ( count < 0 )
{
throw new ArgumentException(SR.IntegerCollectionLengthLessThanZero);
}
theCollection = new Int32Collection( count );
if ( type == IntegerCollectionType.Consecutive )
{
// Get the first integer
int first = reader.ReadInt32();
for( int i = 0; i < count; i ++)
{
theCollection.Add( first + i );
}
}
else
{
switch( type )
{View on GitHub (pinned to 81131a70a4)