Skip to content

Conversation

@stephentoub
Copy link
Member

I went back and forth on this initially, but decided it makes more sense to be conservative here and only recommend using ArgumentNullException.ThrowIfNull if the type of the parameter is known to be a class. While it could be used for nullable value types or unconstrained generics, both result in boxing that we then rely on the JIT to eliminate.

@stephentoub stephentoub requested a review from a team as a code owner December 9, 2022 17:35
@codecov
Copy link

codecov bot commented Dec 9, 2022

Codecov Report

Merging #6314 (85a4a54) into main (22361ae) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #6314   +/-   ##
=======================================
  Coverage   96.07%   96.07%           
=======================================
  Files        1367     1367           
  Lines      315348   315415   +67     
  Branches    10187    10187           
=======================================
+ Hits       302980   303050   +70     
+ Misses       9935     9933    -2     
+ Partials     2433     2432    -1     

Copy link
Member

@Youssef1313 Youssef1313 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider having tests for constrained type parameters:

using System;

class C
{
    void M1<T>(T arg) where T : class
    {
        if (arg is null) throw new ArgumentNullException(nameof(arg));
    }

    void M2<T>(T arg) where T : IDisposable
    {
        if (arg is null) throw new ArgumentNullException(nameof(arg));
    }

    void M3<T>(T arg) where T : C
    {
        if (arg is null) throw new ArgumentNullException(nameof(arg));
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants