dotnet/maui · error · FormatException

Indexer did not contain closing bracket

Error message

Indexer did not contain closing bracket

What it means

Thrown while parsing an indexer segment of a binding path when the part contains '[' but the final character is not ']'. The parser assumes an indexer occupies the end of the part and requires a matching closing bracket, e.g. Items[0] but not Items[0.

Source

Thrown at src/Controls/src/Core/BindingExpression.cs:244

				p = p.Substring(1);
			}

			string[] pathParts = p.Split(ExpressionSplit);
			for (var i = 0; i < pathParts.Length; i++)
			{
				string part = pathParts[i].Trim();
				if (part == string.Empty)
					throw new FormatException("Path contains an empty part");

				BindingExpressionPart indexer = null;

				int lbIndex = part.IndexOf("[", StringComparison.Ordinal);
				if (lbIndex != -1)
				{
					int rbIndex = part.Length - 1;
					if (part[rbIndex] != ']')
						throw new FormatException("Indexer did not contain closing bracket");

					int argLength = rbIndex - lbIndex - 1;
					if (argLength == 0)
						throw new FormatException("Indexer did not contain arguments");

					string argString = part.Substring(lbIndex + 1, argLength);
					indexer = new BindingExpressionPart(this, argString, true);

					part = part.Substring(0, lbIndex);
					part = part.Trim();
				}
				if (part.Length > 0)
				{
					var next = new BindingExpressionPart(this, part);
					last.NextPart = next;
					_parts.Add(next);
					last = next;
				}

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Ensure every '[' in a path segment is paired with a trailing ']'.
  2. Validate indexer segments so each '[' is immediately followed by a non-empty argument and a trailing ']' before binding.
  3. Build indexer paths with a helper that always appends the closing bracket.

Example fix

// before
var path = $"Items[{index}"; // missing ]

// after
var path = $"Items[{index}]";
Defensive patterns

Strategy: validation

Validate before calling

using System.Text.RegularExpressions;
static bool HasBalancedIndexers(string path) =>
    !Regex.IsMatch(path, @"\[[^\]]*$"); // no unclosed '[' at end of a segment
if (!HasBalancedIndexers(path)) throw new ArgumentException("Malformed indexer");

Type guard

static bool IsValidIndexerSegment(string seg) =>
    !seg.Contains('[') || seg.TrimEnd().EndsWith("]");

Prevention

When it happens

Trigger: Paths like "Items[0", "List[abc", or "Dict[key" — any indexer where the closing bracket is missing. Paths built by string concatenation that drop the closing bracket.

Common situations: Programmatic path construction with interpolated indices that omit ']'. Truncation of paths from external sources. Typo in XAML binding paths.

Related errors


AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13). Data as JSON: /api/errors/8c00ed33537ae2fb. Report an issue: GitHub.