I'd say the second way is indeed more precise - yes, it's more cumbersome but you can always wrap it in a method to avoid having to do it all the time. It could even be an extension method:
str.ThrowIfNullOrEmpty("str");
public static void ThrowIfNullOrEmpty(this string value, string name)
{
if (value == null)
{
throw new ArgumentNullException(name);
}
if (value == "")
{
throw new ArgumentException("Argument must not be the empty string.",
name);
}
}
Another form which is potentially useful is one which returns the original string if everything is okay. You could write something like this:
public Person(string name)
{
this.name = name.CheckNotEmpty();
}
Another option to consider is using Code Contracts as an alternative to throwing your own exceptions.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…