1

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.

4
  • 1
    If it is a copy/paste of the code, it might be because the first function is called ThrowIfNullOrEmtpy, not ThrowIfNullOrEmpty. Commented Oct 17, 2013 at 8:53
  • Are you sure? We cant reproduce the problem Commented Oct 17, 2013 at 8:53
  • @SWeko ; You're right. It's a typo. Do you want to create and answer for the points Commented Oct 17, 2013 at 8:58
  • 1
    This question appears to be off-topic because it is too localized (there was a non-obvious typo) Commented Oct 17, 2013 at 9:01

5 Answers 5

1

I've tried the code, and if both methods are indeed overloads, the generic version is (correctly) used.

In the code that is pasted, the first function is called ThrowIfNullOrEmtpy, not ThrowIfNullOrEmpty, so it is a simple misspelling.


This LINQPad code:

void Main() { CreateEmail(new EmailOptions()); } private void CreateEmail(EmailOptions options) { ParameterGuard.ThrowIfNullOrEmpty(options.To, "To"); } public static class ParameterGuard { public static void ThrowIfNullOrEmpty<T>(T enumerable, string argName) where T : IEnumerable { Console.Write("Generic Version"); } public static void ThrowIfNullOrEmpty(string arg, string argName) { Console.Write("String Version"); } } public class EmailOptions { public List<string> To { get; set; } } 

returns "Generic Version"

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks... Slowly bangs head against desk :-)
@PhilMurray, why use two methods? string also implements IEnumerable (and IEnumerable<char>)!
0

Instead of using a generic method like you used, you a normal method:

public static void ThrowIfNullOrEmtpy(IEnumerable enumerable, string argName); 

The generic is not necessary.

Second of all. The methods have different names. It might just be a typo:

ThrowIfNullOrEmtpy vs ThrowIfNullOrEmpty

2 Comments

I have changed it as above but it still does not compile with the same exception
@PhilMurray Did you try to fix the spelling?
0

Why don't you try this:

public static void ThrowIfNullOrEmpty<T>(IEnumerable<T> enumerable, string argName) { ... } 

As mentioned above you will have a mistake in you first method name.

Comments

0

string also implements IEnumerable, you can use only one method:

public static class ParameterGuard { public static void ThrowIfNullOrEmpty<T>(IEnumerable<T> enumerable, string argName) { if (enumerable == null) throw new ArgumentNullException(argName); if (!enumerable.Any()) throw new ArgumentException(); } } public class Program { static void Main(string[] args) { List<string> list = new List<string>(); list.Add("test"); ParameterGuard.ThrowIfNullOrEmpty(list, "list"); ParameterGuard.ThrowIfNullOrEmpty("string", "str"); } } 

Comments

0

Change your methods signature as follows:

public static class ParameterGuard { public static void ThrowIfNullOrEmpty(IEnumerable enumerable, string argName) { } public static void ThrowIfNullOrEmpty(string arg, string argName) { } } 

Your two methods had a small typo and that is what was probably causing the confusion.. This way you should call the method with the enumerable.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.