views:

42

answers:

2

I can add the statement using System.Configuration; to my code using intellisense, but I can't get any intellisense when I access the type ConfigurationManager.

Why does intellisense work when entering the using directive, but doesn't work when specifying a type from that namespace?

using System.Configuration;

namespace TestDBMSConnection
{
    class Program
    {
        static void Main(string[] args)
        {
            var dataProviderName = ConfigurationManager.AppSettings["provider"];
        }
    }
}

I can fix the issue by adding a reference to System.Configuration, but I don't understand why I don't need to do that for intellisense to work for the using directive.

+3  A: 

Classes in a namespace are not required to be located in the same assembly.

Some classes in the System.Configuration namespace are located in System.dll (such as SettingsBase) while some other classes are located in System.Configuration.dll (such as ConfigurationManager).

IntelliSense can only suggest classes and namespaces in referenced assemblies. So if you have System.dll referenced but not System.Configuration.dll, IntelliSense can suggest the System.Configuration namespace and the System.Configuration classes located in System.dll, but not those located in System.Configuration.dll.

IntelliSense can also not know in which unreferenced assembly a certain class might be located. So you have to reference System.Configuration.dll manually before you can use it.

dtb
To test this out I created a console app and removed all references. I could compile and run it. Using intellisense I could create the directive using System.Configuration. I didn't expect to be able to do that since there was no reference to System. Is there some automatic reference to an assembly that references System.Configuration?
Martin Jasper
@Martin Jasper: The *mscorlib.dll* assembly is always implicitly referenced and contains some classes in the System.Configuration.Assemblies namespace.
dtb
Mscorlib.dll contains some objects found in the namespace System.Configuration.Assemblies. Mscorlib is referenced automatically unless you uncheck the option to have it in project settings. It's also possible that intellisense is itself looking into base .NET assemblies like mscorlib, System, etc.
Tergiver
@dtb: I can see System.Core and mscorlib are automatically added to my project by clicking on add reference and seeing that they're selected even though I don't see them listed in Solution Explorer. Using ildasm I can see that System.core doesn't have a ref to System.Configuration, but mscorlib does via System.Configuration.Assembly as mentioned by @Tergiver. Thanks for the help, I understand what's going on now.
Martin Jasper
+2  A: 

Because intellisense forusing shows you the list of known namespaces. And System.Configuration namespace is already referenced(Some of the classes in this namespace are in system.dll or mscorlib.dll), while the ConfigurationManager class is in System.Configuration.dll which is not referenced, and thus intellisense does not know about it.

Alex Reitbort
Thanks Alex. Is there any specific doc/blog/code you use for gaining insight into how intellisense works?
Martin Jasper
http://stackoverflow.com/questions/835682/how-does-intellisense-work-in-visual-studio
Alex Reitbort