dotnet/wpf · error · ArgumentNullException

throw new ArgumentNullException( nameof(xmlnsDictionary));

Error message

throw new ArgumentNullException( nameof(xmlnsDictionary));

What it means

The XmlnsDictionary copy constructor throws ArgumentNullException when the source XmlnsDictionary is null. A new dictionary is always built by copying from an existing instance, so a null source has no valid meaning.

Solutions

  1. Ensure the source XmlnsDictionary is instantiated before copying
  2. Check for null before invoking the copy constructor and create a fresh empty XmlnsDictionary instead when the source is absent

Example fix

// before
var copy = new XmlnsDictionary(sourceDict);

// after
var copy = sourceDict != null ? new XmlnsDictionary(sourceDict) : new XmlnsDictionary();
Defensive patterns

Strategy: type-guard

Type guard

bool CanCopy(XmlnsDictionary source) => source != null;
var copy = CanCopy(source) ? new XmlnsDictionary(source) : new XmlnsDictionary();

Try / catch

try { var copy = new XmlnsDictionary(source); }
catch (ArgumentNullException) { var copy = new XmlnsDictionary(); }

Prevention

When it happens

Trigger: Calling new XmlnsDictionary((XmlnsDictionary)null), e.g. cloning a dictionary stored in a field or property that was never initialized.

Common situations: Copying a dictionary obtained from a ParserContext whose XmlnsDictionary property was not yet populated, or cloning before an object's dictionary member was assigned.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/12f1ef6fbb2af5ef. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XmlnsDictionary.cs:96

        /// </summary>
        public XmlnsDictionary()
        {
            // Initializes the XmlnsDictionary NamespaceDeclarations array with default 8 items
            // and initializes the last Declaration marker to zero for first insertion.
            Initialize();
        }

#if !PBTCOMPILER
        /// <summary>
        /// Construct an XmlnsDictionary based on an already existing one. The Sealed property is not
        /// propagated between dictionaries.
        /// </summary>
        /// <param name="xmlnsDictionary">The dictionary on which to base the new one</param>
        public XmlnsDictionary(XmlnsDictionary xmlnsDictionary)
        {
            if(null == xmlnsDictionary)
            {
                throw new ArgumentNullException( nameof(xmlnsDictionary));
            }
            
            // Copy the Declarations if they exists 
            if (xmlnsDictionary != null && xmlnsDictionary.Count > 0)
            {
                _lastDecl = xmlnsDictionary._lastDecl;
                if (_nsDeclarations == null)
                {
                    _nsDeclarations = new NamespaceDeclaration[_lastDecl+1];
                }

                // Initialize the count to Zero and start counting.
                _countDecl = 0;
                
                for (int i = 0; i <= _lastDecl; i++)
                {
                    // We copy the entire Dictionary, but update the count only for non-null uri/
                    // The reason is, Count, doesn't make sense when we are implementing the 

View on GitHub (pinned to 81131a70a4)