This article is a mirror article of machine translation, please click here to jump to the original article.

View: 2759|Reply: 0

[Source] .NET/C# method parameter NULL null

[Copy link]
Posted on 6/26/2023 8:51:24 PM | | | |
Requirements: You need to determine whether the parameter value passed by the method is null and NULL, and if it is NULL, an exception is thrown.

Generally, we can determine whether the parameter value is NULL, usually the code is as follows:

Although we can achieve the effect we want, the code is not elegant enough, and programmers try to write less code to implement it, and spend time on more meaningful things.

The .NET 7 runtime provides ArgumentNullException.ThrowIfNull, ArgumentException.ThrowIfNullOrEmpty to determine if the parameter is NULL or an empty string throws an exception.

ArgumentNullException.ThrowIfNull(This method is already available in .NET 6):The hyperlink login is visible.
ArgumentException.ThrowIfNullOrEmptyThe hyperlink login is visible.

The test code is as follows:



The source code is as follows:



As for [CallerArgumentExpression] feature is actually syntactic sugar, which is already determined at the time of compilation.



(End)





Previous:.NET/C# HttpClient source code simple analysis
Next:.NET/C# collection Any() or Count() which is faster
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com