12

I have this code in my custom MembershipProvider:

public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
{
    if (config == null)
        throw new ArgumentNullException("config");

    if (name == null)
        name = "MyCustomMembershipProvider";
    ...
}

Resharper marks the second if-Statement and tells me, it would always evaluate to false.

resharper message

But why would this always evaluate to false? I could easily pass null to the method as a parameter.

Is this a bug or is Resharper right here?

PS 1: I use Resharper 6.1
PS 2: I know using string.IsNullOrEmpty() would be the way to go here anyway. I'm just curious.

magnattic
  • 12,638
  • 13
  • 62
  • 115
  • 1
    From which class is your class deriving? Is it one from you or one from the .NET framework? – Daniel Hilgarth Jul 17 '12 at 19:44
  • Is that your complete code? I'm using ReSharper 6.1 and don't get that indication. – D Stanley Jul 17 '12 at 19:46
  • I am deriving from MembershipProvider (in System.Web.Security) – magnattic Jul 17 '12 at 19:47
  • 2
    [`ProviderBase.Initialize`](http://msdn.microsoft.com/en-us/library/system.configuration.provider.providerbase.initialize.aspx) throws also an `ArgumentNullException` when `name` is `null` (and an `ArgumentException` if it's empty). So i assume that Resharper knows that and assumes itself that a child shouldn't be less restrictive. – Tim Schmelter Jul 17 '12 at 20:44

1 Answers1

15

Probably, the argument name is marked as [NotNull]. Resharper ships with contract metadata for common BCL classes.

usr
  • 168,620
  • 35
  • 240
  • 369
  • 1
    Is there any reason it would mark this method parameter this way? It's a public method, what would stop anyone from calling it with a null parameter? – magnattic Jul 17 '12 at 19:49
  • The contract metadata is not meant to be 100% accurate. It is meant to be pragmatic. I think that's a good thing. – usr Jul 17 '12 at 19:49
  • Interestingly, ProviderBase ALSO does a NULL check for name despite the [NotNull] code contract. – D Stanley Jul 17 '12 at 20:02
  • Does this mean that the declaration of the `virtual` method in the base class decorates the first parameter with an attribute called `[NotNull]`? Or where is it that `[NotNull]` is specified? – Jeppe Stig Nielsen Jul 17 '12 at 22:08
  • I don't know. Of course, it is not in mscorlib.dll because that is shipped by Microsoft. Must be some external file that ships with R#. R# can take the metadata from wherever it likes, it does not need to play by the rules of csc.exe. – usr Jul 17 '12 at 22:27