My brain is not working at all this morning so I hope someone can clear this up for me.
I have a little helper method that checks if an IEnumerable is null or has no items.
public static class ParameterGuard { public static void ThrowIfNullOrEmtpy<T>(T enumerable, string argName) where T : IEnumerable { if (enumerable == null) throw new ArgumentNullException(argName); if (enumerable.HasCountOf(0)) throw new ArgumentException(Resources.ExceptionEnumerableEmpty, argName); } public static void ThrowIfNullOrEmpty(string arg, string argName) { if (arg == null) throw new ArgumentNullException(argName); if (arg.Length == 0) throw new ArgumentException(Resources.ExceptionStringEmpty, argName); } } I also have a small data structure for sending emails
public class EmailOptions { public string Server { get; set; } public int Port { get; set; } public string From { get; set; } public List<string> To { get; set; } public List<string> Cc { get; set; } public string Subject { get; set; } public string Body { get; set; } public bool IsHtml { get; set; } } When I try to validate the To property using the ThrowIfNullOrEmpty method I get an exception.
private MailMessage CreateEmail(EmailOptions options) { ParameterGuard.ThrowIfNullOrEmpty(options.To, "To"); ... } Exception
The best overloaded method match for ParameterGuard.ThrowIfNullOrEmpty(string, string)' has some invalid arguments I would have thought that as the IList<> class implements IEnumerable that this would work.
I would appreciate someone clearing this up for me.
ThrowIfNullOrEmtpy, notThrowIfNullOrEmpty.