Runtime: Make Environment.Version returns 4 fields instead of 3 fields to avoid exception handling.

Created on 28 Apr 2020  路  2Comments  路  Source: dotnet/runtime

My Suggestion

Make Environment.Version returns a 4 fields instance of Version such as 3.100.320.12801 instead of 3 fields instance such as 3.1.201.

Why I want the suggestion?

When we call Version.ToString(fieldCount) an ArgumentException might throw out if the Version has only 3 version fields. But we have no direct way to detect how many fields the Version instance has. If we want to avoid the exception, we can do one of these two things below:

  1. Check every field value and determine the fieldCount argument to pass into.
  2. Call ToString() method directly instead of the one with fieldCount overload.

The first one causes a lot of checking codes everywhere and the second one limit the usage of the Version(only ToString() is allowed and it is still dangerous to pass the version instance to other modules.)

System.ArgumentException: Argument must be between 0 and 3. (Parameter 'fieldCount')

https://github.com/dotnet/runtime/blob/master/src/libraries/System.Private.CoreLib/src/System/Version.cs#L261-L267

The history of this change

In .NET Framework, the Environment.Version always returned a 4 fields Version instance so many codes may worked well and maybe another module far away had tried to format the version to a special string. And after the .NET Core was invented, the value of this property kept 4.0.30319.42000 unchanged.

After this issue (https://github.com/dotnet/runtime/issues/3271) and the pull request (https://github.com/dotnet/runtime/commit/c8809dc3415dfd7a1499d38c23e2a85334d74188#diff-73f2844915c581aab917cddd369adec4L132-L135) this property started to return a 3 fields value.

Design Discussion area-System.Runtime enhancement

Most helpful comment

Should we consider removing the exception from Version.ToString and using min(requestedNumber, actualNumber)?

All 2 comments

We have done this to use the same .NET runtime version number everywhere. If we were to change this to return 4 part version, we would be again on path to have diverging runtime version number schemes in different parts.

3.100.320.12801

This number is .NET SDK version. It would not appropriate to use for .NET runtime version. It is not even possible - this version number is not available in .NET runtime.

The first one causes a lot of checking codes everywhere

If your project is sensitive to have versions with all 4 components, is it an option to add a wrapper that centralizes this check and fills in the 4th component ?

Should we consider removing the exception from Version.ToString and using min(requestedNumber, actualNumber)?

Was this page helpful?
0 / 5 - 0 ratings