开发者

What's the difference between using statement and adding a reference?

开发者 https://www.devze.com 2023-03-09 07:56 出处:网络
In Visual Studio, when do you have to add a reference to a dll? I always try to have a minimum of references in my projects, I try to only include the ones that are really necessary.

In Visual Studio, when do you have to add a reference to a dll? I always try to have a minimum of references in my projects, I try to only include the ones that are really necessary.

I would think that I only need a reference if I have a using statement in my source. But that's not always enough.

For instance, I have a very simple program that is using System and Microsoft.Practices.EnterpriseLibrary.Data:

using System;
using Microsoft.Practices.EnterpriseLibrary.Data;

public class SimpleConnection {
    private static void Main() {
        var database = DatabaseFactory.CreateDatabase();
        var command =
            database.GetSqlStringCommand(
                "select table_name from information_schema.tables");
        using (var reader = database.ExecuteReader(command)) {
            while (reader.Read()) {
                Console.WriteLine(r开发者_StackOverflow社区eader.GetString(0));
            }
        }
    }
}

I would think I only have to reference System and Microsoft.Practices.EnterpriseLibrary.Data. But that's not true. If I don't reference System.Data, the code won't compile.

The type 'System.Data.Common.DbCommand' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

How can I know beforehand when I have to add a reference to something I'm not using?


References tell the compiler where to look for types to import. Using statements tell the compiler where to look for "full names"

So you can either type

 using System.Text

 StringBuilder sb; 
 // ...

or

 System.Text.StringBuider sb;
 // ...

But either way, you must have a reference to System.dll (or is it mscorlib for StringBuilder?). Without the ref, the compiler doesn't know what types are available.


You have to add a reference to an assembly the class resides in, and any dependencies, that includes

  • return types from other assembly (ie. a method returns a DbCommand)
  • base class or interface from other assembly (ie. a class derives from DbCommand or implements an interface)


1) In order to access a type from an external assembly you HAVE to ADD A REFERENCE to it in your project. This reference tells the compiler what assembly(ies) to include in your project.

2) Importing the assembly, however, is optional. (in C# this is done with the "using..." directive). This tells the compiler where to look to find the type used. If it is not included then, whenever you reference the type from that assembly, you need to fully qualify its namespace along with its name.

EXAMPLE: to use MessageBox class (Show() method) in a console program you HAVE TO add a reference to the System.Windows.Forms assembly then reference it as...
If not importing assembly:
System.Windows.Forms.MessageBox.Show("Hello");
If importing assembly:
MessageBox.Show("Hello");


Adding a reference allows one to use any objects or functionality contained in that DLL.

Once you add a reference, you can use that functionality.

The using clause helps to shorten the code: You can save on typing.

For example:

using System.IO;

I can then write

Directory d = [Code goes here]

If I had the reference and didn't have the using, then I would write

System.IO.Directory d = [Code goes here]

But you need the reference in order to define the using statement or to use that functionality from the DLL.

Now if you add a reference to you code and that reference needs another DLL, then when you compile you would get an error about the missing reference. At design time it marked as well. Some DLLs are standalone (require no other references) while others require multiple references depending on what features or functionality you are using.


Sometimes the references you add have a dependency on another library, therefore you must have that library in your references.


The using indicates which namespace you are directly referencing. It will often be the case that you need to include other references that that assembly references.

The only way is to either do as you do and fix the errors as they occur or check the documentation to see if that lists what the assembly depends on.

I wouldn't worry about unused references. If they're unused then they're not included.


I think the answer in that case is that the code is using an object from System.data namespace. In your example var command is a DBCommand. It is a System.Data reference and is not is System or the Microsoft.Practices.EnterpriseLibary.Data. So it looks like that code also needs a command reference as well. What's GetSqlStringCommand return? A System.Data.DBCommand. Look at this link. [link]http://msdn.microsoft.com/en-us/library/microsoft.practices.enterpriselibrary.data.database.getsqlstringcommand(v=pandp.31).aspx That is why you need the reference to System.Data.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号