2

I have the following two files:

IGlobalApiProvider.cs

using System.Collections.Generic;
using Vert.Slack;

namespace Vert.Interfaces
{
    public interface IGlobalApiProvider
    {
        List<Im> ImList();
    }
}

And the corresponding implementation: SlackApi.cs

using System.Collections.Generic;
using Vert.Interfaces;

namespace Vert.Slack
{
    public class SlackApi : IGlobalApiProvider
    {
        public List<Im> ImList()
        {
            ...
        }
    }
}

Now, Intellisense is telling me that when I use IM in IGlobalApiProvider it's resolving to Im, which is defined in a file named RtmStart.cs which has no namespace declaration. When I use IM in SlackApi.cs, it's resolving to Vert.Slack.Im which is defined in the Vert.Slack namespace in a file named Im.cs. The weird behavior alerted me to the redundant definition, so I removed it and things are working fine.

However, I'm confused about why Visual Studio behaved differently in these two ways. I can tell something was scanning for the class names in a different pattern in the two situations. I can also tell that being used in the same namespace vs being used in a class that uses the namespace seems to be the trigger. What I don't know is what mechanism controls the logic behind this behavior.

Can anyone shed light on this?

Everything you see is contained in Vert.dll, which consists of one project, Vert.csproj

Link to the four files mentioned in this post as they existed at the time of writing.

Jannik
  • 2,310
  • 6
  • 32
  • 61
Matt
  • 1,674
  • 2
  • 16
  • 34
  • Need more information: Are they in the same project or not? – qxg Dec 15 '15 at 04:49
  • Can you show us the definitions of both classes? Seems like it should give you an ambiguity error, as you have `Vert.Slack.Im` defined in two places. It's not a partial class, is it? Are they both `public`? – Rob Dec 15 '15 at 05:07
  • Are you sure you have written that correctly? Having two definitions for `Im` in the namespace `Vert.Slack` would give a compilation error. – Ergwun Dec 15 '15 at 05:09
  • Full context linked in an edit – Matt Dec 15 '15 at 05:37

2 Answers2

0

This has to do with the difference between the global and Vert.Slack namespaces.

The compiler looks for the most explicit namespace with the proper class defined.

In this example, when the compiler looks for the definition of Im in IGlobalInterfaceProvider.cs, there is no namespace defined (or used) in this file that contains the class, but Im is also defined in this file - which is declared in the global namespace.

When the compiler looks for the definition of Im in SlackApi.cs, Im is found in the explicit Vert.Slack namespace, and utilizes that class.

The answer here is a similar topic and may provide more insight.

Community
  • 1
  • 1
Matt
  • 1,674
  • 2
  • 16
  • 34
-1

This may be related to the fact that your namespaces are in the wrong place ;-)

http://www.stylecop.com/docs/SA1200.html

This answer here gives a good explanation: Should 'using' statements be inside or outside the namespace?

Community
  • 1
  • 1
Allan
  • 107
  • 4
  • This doesn't answer my question (I recently updated the namespaces of the two implementing classes), as I am only ever using `using` outside of a namespace (or without a namespace). I'm looking for an answer that outlines *how* C# looks for class names. – Matt Dec 15 '15 at 05:53
  • The accepted answer of the linked question *does* explain exactly how the compiler resolves name ambiguities. However, answers which are just a link are discouraged. If you're not going to take the time to explain what's in the link, then write a comment instead of an answer – ardila Dec 15 '15 at 07:25
  • I'd love to comment but I'm not allowed yet. Just trying to help. – Allan Dec 15 '15 at 11:16
  • @Allan I know, me too! :) You might want to have a look at the links section of the [guide to writing a good answer](http://stackoverflow.com/help/how-to-answer) as well as [referencing others' work](http://stackoverflow.com/help/referencing). – ardila Dec 15 '15 at 14:39