C # namespace BASICS (2)

Source: Internet
Author: User

In a NAMESPACE, we can also use an alias to represent an existing NAMESPACE or some other types of data.

The alias format is as follows:

Using alias = an existing type;

Example: using soholife = System;


Here are a few examples to better understand:

Namespace N1.N2

{

Class {}

}

Namespace N3

{

Using A = N1.N2.;

Class B: {}

}

Here, in N3, A is the alias of N1.N2. A, while N3. B inherits from N1.N2.! Similarly, we can use the following method to achieve the same effect:

Namespace N3

{

Using R = N1.N2;

Class B: R.A {}

}


Here, I want to raise a question. Let's take a look at the following example:

Namespace N1.N2

{

Class {}

}


Namespace N3

{

Class {}

}


Namespace N3

{

Using A = N1.N2.;

}


If we write it like this, is there a problem?

Of course, the answer is yes. Wrong! Because an alias must be unique in the NAMESPACE

Class a {}, we are using A = N1.NE. A; so it must be wrong! However, if we want:

Using B = N1.N2. A; what is the result? Think about it yourself! I won't say much about it!


I thought it could be over. I suddenly found that there was another place that I hadn't made it clear. I just didn't say it. Oh, it seems that I can only go home later. Let's start with the problem:

Namespace N1.N2

{

Class {}

}

Namespace N3

{

Using R = N1.N2;

}

Namespace N3

{

Class B: R.A {}

}

In the above example, what do you think is true? Error?

If I want to answer this question, it's wrong! (Haha, the answer to this question is wrong when I used to make multiple choice questions! But the reason is that it cannot be said !)


I thought it was correct to read the above program first, and then I learned it later!

When an alias is used in a separate unit, the alias can only be used in the unit where it is located (NAMESPACE or other), but cannot be used in other units, so in the above example, when R is used in the second N3, the system will prompt that R is unknown! Of course, if we want to use this method, we still have a way to write the alias R out of N3:

Using R = N1.N2;

Namespace N3

{

Class B: R.A {}

}


I have said so much. If I can fully understand it, I think I should be able to have some ideas about NAMESPACE! If this is the case, my goal is also achieved!

 

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.