{"record":{"id":"d71d8843597ea9ff","repo":"jstedfast/MailKit","slug":"cannot-search-for-an-empty-set-of-unique-identifiers","errorCode":null,"errorMessage":"Cannot search for an empty set of unique identifiers.","messagePattern":"Cannot search for an empty set of unique identifiers\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"MailKit/Search/UidSearchQuery.cs","lineNumber":59,"sourceCode":"\t\t/// Initializes a new instance of the <see cref=\"T:MailKit.Search.UidSearchQuery\"/> class.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// Creates a new unique identifier-based search query.\n\t\t/// </remarks>\n\t\t/// <param name=\"uids\">The unique identifiers to match against.</param>\n\t\t/// <exception cref=\"System.ArgumentNullException\">\n\t\t/// <paramref name=\"uids\"/> is <see langword=\"null\" />.\n\t\t/// </exception>\n\t\t/// <exception cref=\"System.ArgumentException\">\n\t\t/// <paramref name=\"uids\"/> is empty.\n\t\t/// </exception>\n\t\tpublic UidSearchQuery (IList<UniqueId> uids) : base (SearchTerm.Uid)\n\t\t{\n\t\t\tif (uids == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (uids));\n\n\t\t\tif (uids.Count == 0)\n\t\t\t\tthrow new ArgumentException (\"Cannot search for an empty set of unique identifiers.\", nameof (uids));\n\n\t\t\tUids = uids;\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Initializes a new instance of the <see cref=\"T:MailKit.Search.UidSearchQuery\"/> class.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// Creates a new unique identifier-based search query.\n\t\t/// </remarks>\n\t\t/// <param name=\"uid\">The unique identifier to match against.</param>\n\t\t/// <exception cref=\"System.ArgumentException\">\n\t\t/// <paramref name=\"uid\"/> is an invalid unique identifier.\n\t\t/// </exception>\n\t\tpublic UidSearchQuery (UniqueId uid) : base (SearchTerm.Uid)\n\t\t{\n\t\t\tif (!uid.IsValid)\n\t\t\t\tthrow new ArgumentException (\"Cannot search for an invalid unique identifier.\", nameof (uid));","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Search/UidSearchQuery.cs#L41-L77","documentation":"MailKit's UidSearchQuery(IList<UniqueId>) constructor rejects an empty list because an IMAP UID SEARCH with zero UIDs is meaningless — the server would return nothing or error. The constructor validates uids != null and uids.Count > 0 before building the query. Passing an empty collection is treated as a programming mistake rather than a no-op search.","triggerScenarios":"Calling new UidSearchQuery(new List<UniqueId>()) or new UidSearchQuery(new UniqueIdSet()) — i.e., constructing a UID search query from a collection that has zero elements, typically a result of an earlier fetch/search that returned no messages.","commonSituations":"Building searches dynamically from a list of message UIDs gathered earlier (e.g., a previous Search or Fetch), where the earlier step matched nothing; filtering UIDs through a predicate that removed all entries; deserializing persisted UID lists that are empty.","solutions":["Check the collection count before constructing: skip the search (or use a query that matches everything/nothing intentionally) when uids.Count == 0.","Ensure the upstream code that populates the UID list is correct and only invoke UidSearchQuery when at least one UID exists.","If an empty result is legitimate, guard with an early return so the search (and its EmptyFolder/expunge logic) is never issued."],"exampleFix":"// before\nvar query = searchAndDelete ? MailKit.Search.SearchQuery.Uids(uids) : null; // throws when uids is empty\n\n// after\nvar query = uids.Count > 0 ? MailKit.Search.SearchQuery.Uids(uids) : null;","handlingStrategy":"validation","validationCode":"if (uids != null && uids.Count > 0)\n    folder.Search(SearchQuery.Uids(uids));","typeGuard":"bool HasUids(IList<UniqueId> uids) => uids != null && uids.Count > 0;","tryCatchPattern":"try {\n    folder.Search(SearchQuery.Uids(uids));\n} catch (ArgumentException ex) when (ex.ParamName == \"uids\") {\n    // empty UID set: skip search or fall back to alternate query\n}","preventionTips":["Always count-check UID collections collected from prior searches before turning them into a query.","Treat an empty UID list as a domain condition (nothing to do), not a search request.","Centralize query building in a helper that never emits Uid queries for empty sets."],"tags":["mailkit","imap","argument-validation","empty-collection"],"backgroundTag":"empty-required-field","analyzedSha":"9d3859a7855e3e17582c07fd01972b8e262bf176","analyzedAt":"2026-09-15T15:46:11.592Z","contentChangedAt":"2026-09-15T15:46:11.592Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}