dotnet / dotnet/dotnet-api-docs

Inaccurate/incomplete documentation of some Convert.ToString functions

Open
#9,963 1 comment 0 reactions 0 assignees View on GitHub
area-System.Runtime Pri3 untriaged
Dominant language
C#
Stars
949
Forks
1.7k
Avg merge
3d 27m
Merged PRs (30d)
49

Description

Reported [here](https://developercommunity.visualstudio.com/t/Inaccurateincomplete-documentation-of-s/10135879) on Aug 31, 2022 7:20 AM
The documentation of the Convert.ToString(Object, IFormatProvider function) describes the return value as follows:

The string representation of value, or Empty if value is an object whose value is null. If value is null, the method returns null.

The last sentence is not really accurate. Calling Convert.ToString(null, myFormatProvider) does not call the function Convert.ToString(Object, IFormatProvider), but the function Convert.ToString(String, IFormatProvider) instead, which returns null, if the the string parameter is null. In other words: the result is correct, but it is not this method that returns null, but in fact another one. This should perhaps be clarified.

There is however a case when Convert.ToString(Object, IFormatProvider) may indeed return null directly. This case is only implicitely covered by the documentation’s reference to the string representation of the value: the string representation of an object can be null. Since this might come as a surprise to many users, it would be useful in this context to explictly mention when null might be returned, namely when a value object does neither implement IConvertible.ToString nor IFormattable.ToString, but has a ToString() method that may return null.

I would therefore suggest that the documentation of the Convert.ToString(Object, myFormatProvider) return value be modified as follows (or similar):

The string representation of value, which is null if value neither implements IConvertible.ToString nor IFormattable.ToString and its ToString() method returns null,
– or –
Empty if value is an object whose value is null.

Note that trying to call Convert.ToString(Object, IFormatProvider) with a literal value of null results in calling Convert.ToString(String, IFormatProvider), which when called with a string value of null returns null.

There is a similar issue regarding the documentation of the Convert.ToString(Object), that describes the return value in a single sentence as follows:

The string representation of value, or Empty if value is null.

Analogous to Convert.ToString(Object, myFormatProvider) the same clarifcations might be useful here. I would therefore suggest that the documentation of the Convert.ToString(Object) return value be modified as follows (or similar):

The string representation of value, which is null if value neither implements IConvertible.ToString nor IFormattable.ToString and its ToString() method returns null,
– or –
Empty if value is an object whose value is null.

Note that trying to call Convert.ToString(Object) with a literal value of null results in calling Convert.ToString(String), which when called with a string value of null returns null.

I am attaching here a test project that demonstrats the results of the conversion in the cases discussed above:
[ToStringTest.zip](https://aka.ms/dc/file?name=B40f36b5f4a1b4a5cacc714cfaabb776b637975458720348180_ToStringTest.zip&tid=40f36b5f4a1b4a5cacc714cfaabb776b637975458720348180)

Remark: My formulations assume that IConvertible.ToString and IFormattable.ToString in accordance with their signatures never return null. It should perhaps be double checked if this is really correct. If not, they first sentence of both passages need to reformulated to cover the cases, when IConvertible.ToString or IFormattable.ToString return null.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.