I have a method where it checks if a property is null or not. I know how to throw an arguement null exception if the object is null but how do you throw an arguementnullexception for a property on the object.
private int CalculateCompletedDateDiff(Recall recall)
{
if (!recall.StartDate.HasValue)
{
throw new ArgumentNullException("recall.StartDate");
}
//calculate and return
}
I am using resharper and there is purple scriggly under recall.StartDate
that says cannot resolve symbol. So, if the StartDate cannot be null what is the correct way to throw an arguementnull exception on the startdate property?
You shouldn't throw ArgumentNullException
if the argument (recall
) isn't null. Just ArgumentException
is appropriate here when it's some problem with the argument other than it being null:
if (recall == null)
{
throw new ArgumentNullException("recall");
}
if (recall.StartDate == null)
{
throw new ArgumentException("StartDate of recall must not be null",
"recall");
}
See more on this question at Stackoverflow