Let's say I have a method:
public void SayHello(User user) { if (user == null) throw new ArgumentNullException("user"); Console.Write(string.Format("Hello from {0}", user.Name)); }
Clearly, I should use ArgumentNullException as shown above to verify that the user is not null. Now, how can I verify that user.Name is not empty? It would be good practice to do this:
if (string.IsNullOrWhiteSpace(user.Name)) throw new ArgumentNullException("user", "Username is empty");
No, you should not throw an ArgumentNullException for this purpose, as it is specifically designed to access links null.
null
, null (Nothing in Visual Basic) , .
ArgumentException . InvalidUserException - .
InvalidUserException
msdn .
ArgumentNullException ArgumentException. , , , . , ,
, , null ArgumentNullException ArgumentException .
ArgumentNullException
ArgumentException
, - User . User , Name null.
User
Name
ArgumentException, , ArgumentNullException :
if (string.IsNullOrWhiteSpace(user.Name)) throw new ArgumentException("Username is empty", "user");
Source: https://habr.com/ru/post/1569300/More articles:How to overload → operator in C ++ - c ++Retrieving timezone information from a query in Spring 4.0 - timezoneImplementing a recursive proxy template in C ++ 11 - c ++C ++ 11 && operator (R-value reference) deprecated proxy object design template? - design-patternsQuick way to find the first unused key in a SortedDictionary? - c #What do the words "undefined" in paragraph 5.5.19 / 2.3 of the bullet point in the N4140 mean? - c ++что требует ( "child_process" ) на самом деле? - node.jsStrange memory behavior on the agenda - javascriptPHP generates an incorrect next payment date from December 29 - dateHow to avoid overflow of variable multiplication? - javaAll Articles