In a namespace, we can also use an alias to refer to an existing namespace or some other type of data.
Aliases are used in the following format:
Using alias = a type that already exists;
Example: Using Soholife = System;
Let's take a look at some examples to deepen our understanding:
Namespace N1. N2
{
Class A {}
}
Namespace N3
{
Using A = N1. N2. A
Class B:a {}
}
Here, in N3, A is the alias of N1.n2.a, and n3.b inherits from n1.n2.a!. We can do the same thing in the following way:
Namespace N3
{
Using R = N1. N2;
Class B:r.a {}
}
Say here, I want to ask a question, first look at the following example:
Namespace N1. N2
{
Class A {}
}
Namespace N3
{
Class A {}
}
Namespace N3
{
Using A = N1. N2. A
}
If we write like this, will there be a problem?
The answer is certainly yes, wrong! Because an alias must be unique in namespace,
Class a{}, we are using using a =n1.ne. A; So it must be a mistake! But if we should become:
Using B =n1. N2. A; What about the results? Think of your friend's door! I'm not going to say much!
Originally thought can end, suddenly found that there is still a place has not been said clearly, can say is not said, hehe, it seems only late to go home, I still look at the door from the problem:
Namespace N1. N2
{
Class A {}
}
Namespace N3
{
Using R = N1. N2;
}
Namespace N3
{
Class B:r.a {}
}
In the above example, do not know how people feel, right? Error?
If I were to answer, I'm wrong! (hehe, used to do the habit of choosing questions, and the answer to this question is wrong!) But the reason is not to say! )
Really above this program, first start to see I also thought is correct, later know ... , alas, it seems that we should read more books!
When using an alias in a separate unit, the alias is only available in the unit in which it resides (namespace or otherwise), but not in other cells, so the above example, when using R in the second N3, will prompt R unknown! Of course, if we want to use this way, we still have a way, is to write the alias R outside the N3: the following
Using R = N1. N2;
Namespace N3
{
Class B:r.a {}
}
Well said so much, if can fully understand, I think should be able to properly have some concept of namespace! If I can do that, my purpose is reached!
The above is C # inside the namespace Foundation (ii) content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!