C # about namespace a little introduction

Source: Internet
Author: User
Tags define new features

(a) Namespace explanation:
I feel now learn C # is to know, C # basic Grammar, C # new features, C # can do!
I feel that no matter how, namespace is very important, can be said not only for C #, but the whole. NET is made up of namespace, so I saw the basic grammar of C #, went straight to namespace, write down some of their feelings here, I hope to be able to help the people behind the study.


namespace in the new net environment programming can be said to be ubiquitous, in short, give me the feeling is the core of MS's next generation language is namespace, we can through already have namespace, do what we want to do and willing to do, Of course, if you feel that the existing is not enough for your use, you can also use the existing to expand, create your own namespace! (How does it feel like the previous COM, DCOM?) )

Let me tell you how to build a namespace!.
Define a namespace first need to include keywords: namespace
The format is as follows:
Namespace Your_nsname
{
Namespase main content;
}

Oh, give me feel like class or struct the same. But they in addition to the form has similar, indeed in many aspects are not the same, specific we will slowly speak! Look down first ...

In the body of a namespace, you can refer to the other namespace! For example:
Namespace your_nsname{
The following refers to system and System.Xml with two namespace;
Using System;
Using System.Xml;

Namespase main content;
}
One of the things you should be aware of is that if you want to refer to namespace, you should refer to the other type before declaring it, as follows:
Namespace your_nsname{
some other content;

Since references to system and System.Xml are placed after other statements, ...
Using System;
Using System.Xml;
}

Another interesting place for namespace is ...
Let's take a look at the following two ways:

Way One,
Namespace N1. N2
{class A {}
Class B {}
}

Mode Two,
Namespace N1
{
Namespace N2
{
Class A {}
Class B {}
}
}

In the above varieties, the second is easy to understand, is in namespace N1 to create a namespace N2, and N2 has two classes (class) A, B! What about the first kind? In fact, the above two ways to define the exact same. namespace can be nested defined, we can use the second way, the level is more clear, you can use the first way, the difference is, in the first way, between N1 and N2 must use the separator. To show the hierarchical relationship between them!
Use it in the following ways:
N1. N2. A
N1. N2. B

In a namespace, we can also use an alias to refer to an existing namespace or some other type of data.
The use format for aliases is as follows:
The Using alias = an already existing type;
For example: using Soholife = System;

Here are a few 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! the same way we can get the same effect:
Namespace N3
{
Using R = N1. N2;
Class B:r.a {}
}

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 this, will there be a problem?
Of course the answer is yes, wrong! Because an alias must be the only one in the namespace, and because there is already
Class a{}, we are using a using a =n1.ne. A; So it must be a mistake! But if we should be:
Using B =n1. N2. A; Then what's the result? A friend of the door to think about it! I will not say more!

Originally thought can end, suddenly found that there is still a place has not said clearly, can say is not said, oh, it seems only late home, I door or from the question to see:
Namespace N1. N2
{
Class A {}
}
Namespace N3
{
Using R = N1. N2;
}
Namespace N3
{
Class B:r.a {}
}
In the above example, I do not know what you think, right? Error?
If I were to answer, wrong! (Oh, before the habit of making choice questions, the answer to this question is wrong!) But the reason is not to say it! )

Really above this program, first start to see I also thought is correct, later to know ... , alas, it seems to be more reading!
When using an alias in a separate cell, the alias is only available in the cell in which it is located (namespace or otherwise), but it is not sufficient in other cells, so in the example above, R is not known in the second N3. Of course, if we want to use this kind of way, we still have the way, is to write alias R to N3 outside: the following
Using R = N1. N2;
Namespace N3
{
Class B:r.a {}
}



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.