Relationship of references and using

Source: Internet
Author: User

Refers to an assembly, using a namespace.

To put it simply:
Adding a reference to a project is the path to the referenced file, using the activation of the corresponding namespace in the code. In short, there are two different concepts: one is Project file-related and one is syntax-related.

The first explanation:
Adding a reference is a required condition for using
Namespaces within this assembly can be used only if a reference to the assembly is added
What is the concept of a project adding a reference to introduce an assembly assembly?
An assembly is a DLL, which is a prerequisite for calling a class where an assembly includes one or more namespaces for example
namespace System.Data from File System.Data.DLL
Namespace System.Data.SqlClient also comes from System.Data.DLL
That means you once added a reference to the System.Data assembly file (whether you added the DLL to the project from the GAC or added it directly)
You can use the various classes under System.Data and System.Data.SqlClient
Why using it?
Simply put, you don't even have to write a using, where you need to use a fully qualified name for the type.
What is a fully qualified name?
For example, the DataSet class DataSet:System.Data.DataSet is its fully qualified name.
For example, the database connection class SqlConnection:System.Data.SqlClient.SqlConnection is its fully qualified name.
The advantage of using using is that you don't have to write fully qualified names everywhere, which makes the code look too verbose. Speaking of which, the role of using is understood? In fact, it has played a shorthand role.
The real citation work is done initially, using just to make your subsequent coding work simple and easy.
As you mentioned, the need to use using can be used,
After all, because the class you are using writes an incomplete class name (that is, a non-fully qualified name), the system does not know where the class came from,
The system will find the source of this class in all the using, and if there is no using, nature cannot find it.
Last Example:
You need to define a data set,
Method One
Using System.Data; And then
DataSet MySet = new DataSet ();
Method Two: No using
Wk_ad_begin ({pid:21}); Wk_ad_after (, function () {$ ('. Ad-hidden '). Hide ();}, function () {$ ('. Ad-hidden '). Show ();});
Direct
System.Data.DataSet MySet = new System.Data.DataSet ();
Of course, both methods require you to add a reference to the System.Data to the project, both of which are feasible, but do you feel that the first is more concise
Practice It yourself!

The second explanation:
A reference is a reference to another assembly by an assembly, and a refers to B, which indicates that a must exist for a B assembly to function properly, possibly for reasons such as: Resource, code, and so on; If you do not find this Assembly B, assembly A will fail to start.
Using, in a unit of code, represents a reference to a namespace that allows your code to use the name in that namespace without the need for a full name.
For example: In one of the assembly B you are referencing, a piece of code is defined like this:
Namespace b.1.2.3.4.5.6.7
{
public class bclass{}
}
Of course the namespaces in the actual application may not be so complicated, and I write so much just to explain why use using.
In one of the code units in your assembly A, you have two ways of using this bclass,
Scenario 1: Using the full name
B.1.2.3.4.5.6.7.bclass BC = new B.1.2.3.4.5.6.7.bclass (); Use the full name: namespace. Class Name
Scenario 2: Use using
Using b.1.2.3.4.5.6.7; "Introduce" this namespace
Bclass BC = new B.class ();
Using also has a special usage that does not introduce a namespace, but an alias. Use, let me give an example.
Namespace b.1.2.3.4.5.6
{
public class string{}
}
A string class is defined in the b.1.2.3.4.5.6 space, and the code for the class cannot be modified (mainly the name of the class string cannot be modified).
If you need to use this class, you can use the above mentioned scenario 1, but particularly troublesome. Because the namespace is too long.
If you use Scenario 2, the String class will conflict with the system's string, causing all strings to be used without compiling.
The use of this special usage to solve this problem is this:
Using b.1.2.3.4.5.6 = B6;
The above code defines an alias B6 for the namespace.
Then you use the class string in the code in the way of scenario one, but you can use it like this:
B6. String s = new B6. String ();
Isn't that a lot easier?
Of course you can still use the full name.
b.1.2.3.4.5.6 s = new b.1.2.3.4.5.6.string ();
This method of use is always valid.
Having said so much, I hope you have a clear understanding of it.



Relationship of references and using

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.