Namespace basics in c# (2)

黄舟
Release: 2016-12-19 10:32:16
Original
1228 people have browsed it

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

The format for using aliases is as follows:

using alias = an existing type;

For example: using soholife = System;


Let’s deepen our understanding through a few examples:

namespace N1.N2

{

class A {}

}

namespace N3

{

using A = N1.N2.A;

class B: A {}

}

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

namespace N3

{

using R = N1.N2 ;

class B: R.A {}

}


Speaking of which, I would like to ask a question. Let’s look at the following example first:

namespace N1.N2

{

class A {}

}


namespace N3

{

class A {}

}


namespace N3

{

using A = N1.N2.A;

}


If we write like this, will there be any problem?

The answer is of course yes, wrong! Because an alias must be unique in NAMESPACE, and since we already have

class a{} above, we are using using A =N1.NE.A;, so something must go wrong! But if we should:

using B =N1.N2.A; then what is the result? Friends, think about it for yourself! I will not say much!


I thought it was over, but suddenly I found that there is still one thing that has not been explained clearly. It can be said that I have not said it clearly. Haha, it seems that I can only go home later. Let’s start with the problem:

namespace N1.N2

{

class A {}

}

namespace N3

{

using R = N1.N2;

}

namespace N3

{

class B: R.A {}

}

In the above example, I don’t know what you think, is it correct? mistake?

If I were to answer, it would be wrong! (Haha, I have developed a habit of doing multiple-choice questions in the past. Any answer to such a question is wrong! But I can’t explain the reason!)


Really, the above program, I thought it was correct when I first looked at it, but then Just known. . . , Alas, it seems that I still need to read more books!

It turns out that when using an alias in a separate unit, the alias can only be used in the unit where it is located (NAMESPACE or other), but not in other units. So in the above example, in the When using R in two N3, it will prompt that R is unknown! Of course, if we want to use this method, we still have a way, which is to write the alias R outside N3: as follows

using R = N1.N2;

namespace N3

{

class B: R.A {}

}


Well, after saying so much, if you can understand everything, I think you should be able to have some concepts about NAMESPACE! If this can be done, my goal has been achieved!

The above is the content of namespace basics (2) in c#. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!