Namespace basics in c# (1)

黄舟
Release: 2016-12-19 10:30:54
Original
1094 people have browsed it

I now feel that to learn C# well is to know the basic syntax of C#, the new features of C#, and what C# can do!

Among them, I feel that NAMESPACE is very critical no matter what. It can be said that it is not just for C#, but the whole. NET is composed of NAMESPACE, so after reading the basic syntax of C#, I went straight to NAMESPACE and wrote down some of my feelings here, hoping to be helpful to those who will learn later.


NAMESPACE can be said to be omnipresent in programming in the new NET environment. In short, it gives me the feeling that the core of MS’s new generation language is NAMESPACE. We can do what we want and are willing to do through the existing NAMESPACE. Of course, if you feel that the existing ones are not enough for your use, you can also use the existing ones to expand and create your own NAMESPACE! (Why does it feel so much like the previous COM and DCOM?)


Now let’s talk about how to create a NAMESPACE!

To define a NAMESPACE, you first need to include keywords: namespace

The format is as follows:

namespace Your_nsname

{

//namespase main content;

}


Haha, it feels like it’s like Class or Struct is the same. However, apart from being similar in form, they are indeed different in many aspects. We will talk about the details later! Let’s look down first. . .


In the body of a NAMESPACE, you can reference other NAMESPACE! For example:

namespace Your_nsname{

//The following references System and System.xml with two NAMESPACE;

using System;

using System. One thing to note above is that if you want to reference NAMESPACE, you should reference it before declaring other types. The following is wrong:

namespace Your_nsname{

//Some other content;

//Due to referencing System and System.Xml is placed after other statements, so. . .

using System;

using System.Xml;

}

There is another interesting thing about NAMESPACE, that is. . .

Let’s take a look at the following two methods first:

Method 1,


namespace N1.N2

{ class A {}

class B {}

}

Method 2,

namespace N1

{

namespace N2

{

class A {}

class B {}

}

}

Among the above methods of breeding, the second one is easy to understand, it is Create another NAMESPACE N2 in NAMESPACE N1, and there are two classes A and B in N2! What about the first one? In fact, the definitions of the above two methods are actually exactly the same. NAMESPACE can be defined nested. We can use the second method, which has a clearer sense of hierarchy. We can also use the first method. The difference is that in the first method, a separator must be used between N1 and N2. "." to indicate the hierarchical relationship between them!

Use the following method when using it:

N1.N2.A;

N1.N2.B;

The above is the content of namespace basics (1) in c#. For more related content, please pay attention to PHP Chinese Net (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