-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[API Proposal]: Additional ArgumentNullException.ThrowIfNull overloads #58047
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
I don't think an overload of The |
That is the exact purpose of the proposed API overload, to allow such code to throw if the input is invalid ( |
Tagging subscribers to this area: @dotnet/area-system-runtime Issue DetailsBackground and motivationWhile migrating code to start using API Proposalnamespace System
{
public partial class ArgumentNullException : ArgumentException
{
/// <summary>Throws an <see cref="ArgumentNullException"/> if <paramref name="argument"/> is null.</summary>
/// <param name="argument">The pointer argument to validate as non-null.</param>
/// <param name="paramName">The name of the parameter with which <paramref name="argument"/> corresponds.</param>
public static unsafe void ThrowIfNull(void* argument, [CallerArgumentExpression("argument")] string? paramName = null);
/// <summary>Throws an <see cref="ArgumentNullException"/> if <paramref name="argument"/> is null.</summary>
/// <param name="argument">The argument to validate as non-null.</param>
/// <param name="paramName">The name of the parameter with which <paramref name="argument"/> corresponds.</param>
public static void ThrowIfNull<T>([NotNull] T? argument, [CallerArgumentExpression("argument")] string? paramName = null)
where T : struct;
}
} API Usagepublic static int DereferenceSomePointer(int* pointer)
{
ArgumentNullException.ThrowIfNull(pointer);
return *pointer;
}
public static int DereferenceAnotherPointer(IntPtr pointer)
{
ArgumentNullException.ThrowIfNull((void*)pointer);
return *(int*)pointer;
}
public static int GetValueOfNullableInt(int? nullable)
{
ArgumentNullException.ThrowIfNull(nullable);
return nullable.Value;
} Risks
|
I don't see an issue with the pointer overload as I think it fits for the same scenarios that the existing API was exposed.
|
The comments regarding the |
I've updated the OP to provide a motivating use case for the |
I got your point @tannergooding, since |
Many part of the C# language and runtime is keeping a symmetry between reference-typed nulls and |
I think I would like an overload of ThrowIfNull like so: public static unsafe void ThrowIfNull<T>(params T? argument); Then one can use it like so: internal static void Something(string something, string something2)
{
ArgumentNullException.ThrowIfNull(something, something2);
// snip.
} Limitations for this: all params passed in the single call would have to be the same type, this overload would be uncaring about the parameter names (for those who does not care to pass them into the exception type themselves). |
@AraHaan I think it would be best to wait for |
We think that the namespace System
{
public partial class ArgumentNullException : ArgumentException
{
/// <summary>Throws an <see cref="ArgumentNullException"/> if <paramref name="argument"/> is null.</summary>
/// <param name="argument">The pointer argument to validate as non-null.</param>
/// <param name="paramName">The name of the parameter with which <paramref name="argument"/> corresponds.</param>
public static unsafe void ThrowIfNull(void* argument, [CallerArgumentExpression("argument")] string? paramName = null);
}
} |
Background and motivation
While migrating code to start using
ArgumentNullException.ThrowIfNull
, I'm finding it cumbersome that there are no overloads for passing in nullable value types or pointers. This leads to code looking inconsistent because it uses a mix ofThrowIfNull
and either custom throw helpers or conditionals, it feels like I'm still dealing with all the problems thatThrowIfNull
was added to solve, just for pointers and value types instead of classes.API Proposal
API Usage
Risks
Nullable<T>
overload could potentially result in a lot of generic instantiations.unsafe
method being exposed on an extremely common safe type. I'm not sure how the .NET team feels about this.The text was updated successfully, but these errors were encountered: