Search code examples
c#.netusingusing-statementusing-directives

Having to use a fully qualified class name


For some reason, after adding a reference to MySql.Data and adding using MySql.Data;, I'm unable to type new MySqlConnection() - instead I'm having to type new MySql.Data.MySqlClient.MySqlConnection();.

I can resolve this by adding this to the top of my class:

using MySqlClient = MySql.Data.MySqlClient;
using MySqlConnection = MySql.Data.MySqlClient.MySqlConnection;
using MySqlException = MySql.Data.MySqlClient.MySqlException;

But how can I access all classes within MySqlClient without having to alias them? I haven't seen this behaviour before.


Solution

  • Add

    using MySql.Data.MySqlClient;
    

    Now this works:

    using(var con = new MySqlConnection("..."))
    {
        // ...
    }
    

    You cannot use "sub-namespaces" during initialization of an object with new. So this is not allowed:

    using MySql.Data;
    
    // ...
    using(var con = new MySqlClient.MySqlConnection("..."))
    {
        // ...
    }
    

    because MySqlClient is considered to be a class instead of a part of the namespace. That's why it cannot be resolved and it needs to be qualified fully:

    using(var con = new MySql.Data.MySqlClient.MySqlConnection("..."))
    {
        // ...
    }