System.Security.Cryptography.Oid is functionally init-only

The System.Security.Cryptography.Oid class, which is used to represent ASN.1 Object Identifier values and their "friendly" names, was previously fully mutable. This mutability was often overlooked or came as a surprise. The property setters now throw a PlatformNotSupportedException when you attempt to change the value after it's already been assigned.

Change description

In previous versions, the property setters on Oid can be used to change the value of the FriendlyName and Value properties.

In .NET 5 and later versions, the property setters can only be used to initialize the value. Once the property has a value, either from a constructor or a previous call to the property setter, the property setter always throws a PlatformNotSupportedException.

Reason for change

This change enables the reuse of Oid objects as part of return values in public APIs to reduce object allocation profiles. It avoids the need to create temporary "defensive" copies when Oid values are used as inputs.

Version introduced

5.0

Avoid using the Oid property setters other than for object initialization. To represent a new value, use a new instance instead of changing the value on an existing object.

Affected APIs