About the namespace in C #

Source: Internet
Author: User

Namespace declaration:

UseNamespaceA keyword is used to declare a namespace. The namespace declaration cannot contain any access modifiers. Declaring a namespace can help control the range of class names and method names to prevent name conflicts.

Namespaces can be nested.

For example:

Namespace
{
Namespace B
{

}

}

Namespace A. B
{
    

}

The two are equivalent.

Namespace reference:

 Use the using command to import the namespace or use a fully qualified name. In most cases, you can use using. But how can I cause a compilation error when two namespaces have the same class name,

In this case, you need to use a fully qualified name. Another way is to use using to create an alias for the namespace.

For example:

Declare two namespaces with the same class Test.

Namespace
{
Public class Test
{

}

}
Namespace B
{
Public class Test
{
}
}

Only use a namespace class. You can use the using command or use a fully qualified name.

For example:

Using;

Namespace C
{
Class Program
{
Static void Main (string [] args)
{
Test a = new Test (); // equivalent to the fully qualified name A. Test A = new A. Test ();
}
}
}

If you use both the Test classes of the namespace A and B, the following error occurs: "Test" is an ambiguous reference between "A. Test" and "B. Test ".

Using;

Using B;

Namespace C
{
Class Program
{
Static void Main (string [] args)
{
Test a = new Test ();

}
}
}

In this case, use the fully qualified name to use the class Test.

Using;

Using B;

Namespace C
{
Class Program
{
Static void Main (string [] args)
{
A. Test a = new A. Test ();

}
}
}

In this way, no error is reported.

Related Article

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.