You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Make Assert.IsNotNull generic and have it return the value that was checked #10918
I'm 120% sure that the boat has long sailed for this proposal (with probably very good reasons, such as code side explosion), but I'll try anyway.
It would've been nice if the Assert.IsNotNull() overloads where generic and returned the value that was passed to it.
This would've allowed for nice code like this:
changed the title [-]MAke Assert.IsNotNull generic and have it return the value that was checked[/-][+]Make Assert.IsNotNull generic and have it return the value that was checked[/+]on Sep 2, 2026
The first alternative evaluates fi.Directory again, which can matter for computed or stateful properties. The second is equivalent but requires a separate declaration and assertion. Returning the checked value also aligns with value-returning assertions such as Assert.IsInstanceOfType<T> and Assert.ContainsSingle.
I have prototyped this by adding generic overloads while retaining the existing void IsNotNull(object?, ...) overloads as binary-compatibility shims. The repository builds successfully, and the TestFramework unit tests pass across all target frameworks.
Adding an additional generic overload is risky. It's also possible that it introduces a source breaking change (for F# maybe - needs to be double checked)
If anything to be done here, I would vote for doing a breaking change in v5 to drop the non-generic overload and only have the generic one (binary breaking). But even then I'm not sure it brings enough value to take a breaking change for.
In addition to that, I think it does not make sense for Assert.IsNull to follow the same pattern, and the API will feel weird if IsNull and IsNotNull are not consistent.
The benefit here is ergonomic rather than a capability gap: a temporary variable followed by Assert.IsNotNull is equally safe and evaluates the expression only once.
I agree that it is cleaner to wait for the next breaking release and change the existing API shape, rather than permanently retain void overloads alongside generic value-returning overloads solely for binary compatibility.
The apparent asymmetry with Assert.IsNull is consistent with existing APIs. Assert.IsInstanceOfType<T> and Assert.IsExactInstanceOfType<T> return the successfully narrowed T, while Assert.IsNotInstanceOfType<T> and Assert.IsNotExactInstanceOfType<T> return void. Positive assertions can expose a useful narrowed value; their negative counterparts cannot.
Moving this to the MSTest 5.0 / MTP 3.0 milestone.
Gert Driesen (@drieseng) We tried this for MSTest 5, but downstream integration exposed a usability regression that outweighs the convenience.
In existing C# async methods, Assert.IsNotNull(task); now produces CS4014 because the assertion returns the checked Task. With warnings treated as errors, those existing assertions stop compiling. This broke all 10 build legs in dotnet/sdk#56993; see the original failing build.
Requiring an explicit discard for an ordinary null assertion is not the API experience we want. Adding await is not an equivalent fix: it awaits the checked operation, not a null check, and can change control flow or hang shutdown code when cancellation comes afterwards.
We will keep the original void overloads and their [NotNull] annotations. A local variable followed by the assertion already gives single evaluation and nullable-flow narrowing without changing the assertion's contract.
Amaury Levé (@Evangelink), it's a pity that this won't make the cut, but it's not a total surprise. A new method would have been an option, but coming up with a proper name would be a challenge. The new API would perhaps also introduce confusion.
If only extension members would allow one to override a member that is declared on a type :-)
Thanks for the suggestion, Gert Driesen (@drieseng)! I've reopened this so we can explore a separately named, opt-in API without changing existing Assert.IsNotNull calls.
Assert.GetNotNull is one candidate worth discussing. A custom extension is another option, although extension members cannot override an applicable member already declared on Assert.
I've opened #12306 with Task/Task<T> regression coverage for the plain, string-message, and interpolated-message overloads, so this compatibility issue is caught in the framework unit-test build.
Activity
[-]MAke Assert.IsNotNull generic and have it return the value that was checked[/-][+]Make Assert.IsNotNull generic and have it return the value that was checked[/+]Youssef1313 commentedon Sep 2, 2026
What's the point/value of making this change?
How is that different from either:
or
Evangelink commentedon Sep 2, 2026
The main benefit is that the assertion can be used as a narrowing expression without evaluating the value twice:
The first alternative evaluates
fi.Directoryagain, which can matter for computed or stateful properties. The second is equivalent but requires a separate declaration and assertion. Returning the checked value also aligns with value-returning assertions such asAssert.IsInstanceOfType<T>andAssert.ContainsSingle.I have prototyped this by adding generic overloads while retaining the existing
void IsNotNull(object?, ...)overloads as binary-compatibility shims. The repository builds successfully, and the TestFramework unit tests pass across all target frameworks.Youssef1313 commentedon Sep 2, 2026
Adding an additional generic overload is risky. It's also possible that it introduces a source breaking change (for F# maybe - needs to be double checked)
If anything to be done here, I would vote for doing a breaking change in v5 to drop the non-generic overload and only have the generic one (binary breaking). But even then I'm not sure it brings enough value to take a breaking change for.
In addition to that, I think it does not make sense for
Assert.IsNullto follow the same pattern, and the API will feel weird ifIsNullandIsNotNullare not consistent.Evangelink commentedon Sep 2, 2026
The benefit here is ergonomic rather than a capability gap: a temporary variable followed by
Assert.IsNotNullis equally safe and evaluates the expression only once.I agree that it is cleaner to wait for the next breaking release and change the existing API shape, rather than permanently retain
voidoverloads alongside generic value-returning overloads solely for binary compatibility.The apparent asymmetry with
Assert.IsNullis consistent with existing APIs.Assert.IsInstanceOfType<T>andAssert.IsExactInstanceOfType<T>return the successfully narrowedT, whileAssert.IsNotInstanceOfType<T>andAssert.IsNotExactInstanceOfType<T>returnvoid. Positive assertions can expose a useful narrowed value; their negative counterparts cannot.Moving this to the MSTest 5.0 / MTP 3.0 milestone.
drieseng commentedon Nov 5, 2026
Amaury Levé (@Evangelink), thanks again!! Would it be possible for you to work part-time on the NuGet (Package Manager) team? :)
Evangelink commentedon Nov 6, 2026
Thanks for the warm feedback :) Any particular issue I could poke people about?
Evangelink commentedon Nov 9, 2026
Gert Driesen (@drieseng) We tried this for MSTest 5, but downstream integration exposed a usability regression that outweighs the convenience.
In existing C# async methods,
Assert.IsNotNull(task);now producesCS4014because the assertion returns the checkedTask. With warnings treated as errors, those existing assertions stop compiling. This broke all 10 build legs in dotnet/sdk#56993; see the original failing build.Requiring an explicit discard for an ordinary null assertion is not the API experience we want. Adding
awaitis not an equivalent fix: it awaits the checked operation, not a null check, and can change control flow or hang shutdown code when cancellation comes afterwards.We will keep the original
voidoverloads and their[NotNull]annotations. A local variable followed by the assertion already gives single evaluation and nullable-flow narrowing without changing the assertion's contract.The revert of microsoft/testfx#12219 is prepared in microsoft/testfx#12273, and we won't implement the return-value change to
Assert.IsNotNull.drieseng commentedon Nov 9, 2026
Amaury Levé (@Evangelink), it's a pity that this won't make the cut, but it's not a total surprise. A new method would have been an option, but coming up with a proper name would be a challenge. The new API would perhaps also introduce confusion.
If only extension members would allow one to override a member that is declared on a type :-)
Again thanks for considering this change!
Evangelink commentedon Nov 10, 2026
Thanks for the suggestion, Gert Driesen (@drieseng)! I've reopened this so we can explore a separately named, opt-in API without changing existing
Assert.IsNotNullcalls.Assert.GetNotNullis one candidate worth discussing. A custom extension is another option, although extension members cannot override an applicable member already declared onAssert.I've opened #12306 with
Task/Task<T>regression coverage for the plain, string-message, and interpolated-message overloads, so this compatibility issue is caught in the framework unit-test build.Evangelink commentedon Nov 10, 2026
Alright so with extension methods, we could do something but it's a little cumbersome as it would force declaring the type argument explictly:
Gert Driesen (@drieseng) would it be something fine for you?
drieseng commentedon Nov 20, 2026
Amaury Levé (@Evangelink), I'm afraid there's no good way to deal with this. I'm ok with closing this.
Sorry for not responding any sooner, busy week both professionally and privately :(