Skip to content

OrderedDictionary.Item[Object] throws exception instead of returning null #10924

Closed
@daviesj

Description

@daviesj

Type of issue

Typo

Description

Current documentation for OrderedDictionary says that the Item[Object] property returns:
The value associated with the specified key. If the specified key is not found, attempting to get it returns null, and attempting to set it creates a new element using the specified key.

Actually, if you try to get the property and the key isn't found, it throws KeyNotFoundException.

Example code

OrderedDictionary<string,string> orderedDictionary = new();
if (orderedDictionary["test"] == null)
{
    System.Console.WriteLine("Got null for key not in dictionary");
}

Expected result according to documentation:

Got null for key not in dictionary

Actual result:

Unhandled exception. System.Collections.Generic.KeyNotFoundException: The given key 'test' was not present in the dictionary.
   at System.Collections.ThrowHelper.ThrowKeyNotFound[TKey](TKey key)
   at System.Collections.Generic.OrderedDictionary`2.get_Item(TKey key)
   at Program.<Main>$(String[] args) in D:\Example\Program.cs:line 4

Page URL

https://learn.microsoft.com/en-us/dotnet/api/system.collections.specialized.ordereddictionary.item?view=net-9.0#system-collections-specialized-ordereddictionary-item(system-object)

Content source URL

https://github.com/dotnet/dotnet-api-docs/blob/main/xml/System.Collections.Specialized/OrderedDictionary.xml

Document Version Independent Id

98d00605-bb79-4fd9-9187-97c3067602ed

Article author

@dotnet-bot

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions