-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Description
edited by @tarekgh to add the API proposal.
Description
In .NET 9.0, we introduced overloads for the TimeSpan.From...
methods to accept integral types. Unfortunately, the approved proposal included only a single overload with an optional parameter for the FromMilliseconds
method.
public static TimeSpan FromMilliseconds(long milliseconds, long microseconds = 0);
Unfortunately, this broke the LINQ expression when used as follows, resulting in the error: error CS0854: An expression tree may not contain a call or invocation that uses optional arguments
.
Expression<Action> a = () => TimeSpan.FromMilliseconds(1000); // compiles with .NET 8, fails with .NET 9
Note, this worked fine in net8 because we didn't have the new overload and we had the one that is accepting double
parameter.
Proposal
public partial struct TimeSpan
{
+ public static TimeSpan FromMilliseconds(long milliseconds);
+ public static TimeSpan FromMilliseconds(long milliseconds, long microseconds);
- public static TimeSpan FromMilliseconds(long milliseconds, long microseconds = 0);
}
Original Post
Description
I have seen the breaking change notice with regards to F#, but migrating to .NET 9 has revealed a couple of places in our C# projects, where constructing a TimeSpan is part of a System.Linq.Expressions.Expression. These now don't compile with .NET 9
Reproduction Steps
using System.Linq.Expressions;
Expression<Action> a = () => TimeSpan.FromMilliseconds(1000); // compiles with .NET 8, fails with .NET 9
Expected behavior
Code continues to compile
Actual behavior
Code breaks in .NET 9
Regression?
yes
Known Workarounds
You need to force the double
overload
Expression<Action> a = () => TimeSpan.FromMilliseconds(1000.0);
or provide the optional parameter
Expression<Action> a = () => TimeSpan.FromMilliseconds(1000, 0);
Configuration
No response
Other information
The reason is that the optional parameters are not supported in expressions.