{"record":{"id":"8b33da1368f9a9a5","repo":"jstedfast/MailKit","slug":"the-headerset-is-read-only","errorCode":null,"errorMessage":"The HeaderSet is read-only.","messagePattern":"The HeaderSet is read-only\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"MailKit/HeaderSet.cs","lineNumber":125,"sourceCode":"\t\t\tAddRange (headers);\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Initializes a new instance of the <see cref=\"HeaderSet\"/> class.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// Creates a new <see cref=\"HeaderSet\"/>.\n\t\t/// </remarks>\n\t\t/// <param name=\"headers\">The headers to include.</param>\n\t\tpublic HeaderSet (IEnumerable<string> headers)\n\t\t{\n\t\t\tAddRange (headers);\n\t\t}\n\n\t\tvoid CheckReadOnly ()\n\t\t{\n\t\t\tif (IsReadOnly)\n\t\t\t\tthrow new InvalidOperationException (\"The HeaderSet is read-only.\");\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Get the number of headers in the set.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// Gets the number of headers in the set.\n\t\t/// </remarks>\n\t\t/// <value>The number of headers.</value>\n\t\tpublic int Count {\n\t\t\tget { return hash.Count; }\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Get or set whether this set of headers is meant to be excluded when used with a <see cref=\"IFetchRequest\"/>.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// Get or set whether this set of headers is meant to be excluded when used with a <see cref=\"IFetchRequest\"/>.","sourceCodeStart":107,"sourceCodeEnd":143,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/HeaderSet.cs#L107-L143","documentation":"HeaderSet implements a read-only mode (IsReadOnly). Any mutating method (Exclude, Add, AddRange, Clear, Remove) calls CheckReadOnly, which throws InvalidOperationException when the set is frozen. MailKit marks header sets read-only when they are shared/frozen state that must not change (e.g. a fixed query header list).","triggerScenarios":"Calling Add/AddRange/Remove/Clear/Exclude on a HeaderSet whose IsReadOnly is true.","commonSituations":"Reusing a HeaderSet instance obtained from or attached to an immutable query/frozen state; accidentally freezing a set and later trying to mutate it; sharing one HeaderSet across threads or queries.","solutions":["Clone or create a new mutable HeaderSet instead of mutating the read-only one","Check IsReadOnly before mutating and unfreeze/replace if a mutable setter exists","Construct a fresh HeaderSet and add the desired headers"],"exampleFix":"// before\nreadOnlySet.Add (HeaderId.Subject); // throws\n// after\nvar set = new HeaderSet (HeaderId.Subject);","handlingStrategy":"type-guard","validationCode":"if (set.IsReadOnly)\n    set = new HeaderSet (set.ToArray ()); // work on a mutable copy","typeGuard":"static bool CanMutate (HeaderSet s) => s != null && !s.IsReadOnly;","tryCatchPattern":"try {\n    set.Add (HeaderId.Subject);\n} catch (InvalidOperationException) {\n    set = new HeaderSet (HeaderId.Subject); // recreate mutable set\n}","preventionTips":["Check IsReadOnly before any mutation","Treat shared/frozen HeaderSets as immutable and clone for edits","Avoid sharing HeaderSet instances across queries or threads"],"tags":["csharp","mailkit","read-only","invalid-operation"],"backgroundTag":"unsupported-operation","analyzedSha":"9d3859a7855e3e17582c07fd01972b8e262bf176","analyzedAt":"2026-09-15T15:46:11.592Z","contentChangedAt":"2026-09-15T15:46:11.592Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}