Namespaces

Namespaces are heavily used in C# programming in two ways.

First, the .NET Framework uses namespaces to organize its many classes, as follows:
System.Console.WriteLine("Hello World!");

System  is a namespace and Console is a class in that namespace. The using keyword can be used so that the complete name is not required, as in the following example:

using System;
Console.WriteLine("Hello");
Console.WriteLine("World!");

Second, declaring your own namespaces can help you control the scope of class and method names in larger programming projects. Use the namespace keyword to declare a namespace, as in the following example:

 1 namespace SampleNamespace
 2 {
 3     class SampleClass
 4     {
 5         public void SampleMethod()
 6         {
 7             System.Console.WriteLine("SampleMethod inside SampleNamespace");
 8         }
 9     }
10 }

// -Namespaces have the following properties:
// 1.They organize large code projects.
// 2.They are delimited by using the . operator.
// 3.The using directive obviates the requirement to specify the name of the namespace for every class.
// 4.The global namespace is the "root" namespace: global::System will always refer to the .NET Framework namespace System.
//
//
// Accessing Namespaces
//
// using System;
// Console.WriteLine("Hello,World!");
//
// instead of:
//
// System.Console.WriteLine("Hello,World!");

1 using Alias = System.Console;
2 class TestClass
3 {
4     static void Main()
5     {
6         Alias.WriteLine("Hi");
7         Alias.ReadLine();
8     }
9 }
时间: 2024-10-29 19:09:41

Namespaces的相关文章

.NET Framework posters with Namespaces & Types

Framework is platform containing a huge library of types, methods, classes, etc., cataloged into namespaces. But it is not ending here. With each new version of the framework, a new bunch of features is added to already existing ones. So, it is getti

[单元测试]VS-通过代码添加单元测试提示No classes or namespaces in this assembly

在Visual Studio 2012中,打算给以下方法添加单元测试,但却出现了提示:No classes or namespaces in this assembly的提示. 1 namespace UnitTest 2 { 3 class Program 4 { 5 public int Add(int a, int b) 6 { 7 return a + b; 8 } 9 public int Divide(int a, int b) 10 { 11 return a / b; 12 }

[Quote] 3.6 Namespaces

From http://www.informit.com/articles/article.aspx?p=31783&seqNum=6 This chapter is from the book Navigating C++ and Object-Oriented Design (Bk/CD-ROM) 3.6 Namespaces An earlier section ("Storage Classes" on page 124) explains scope rules fo

Python Scopes and Namespaces

Before introducing classes, I first have to tell you something about Python's scope rules. Class definitions play some neat tricks with namespaces, and you need to know how scopes and namespaces work to fully understand what's going on. Incidentally,

Linux network namespaces

介绍OpenStack neutron使用Linux网络命名空间来避免物理网络和虚拟网络间的冲突,或者不同虚拟网络间的冲突. 网络命名空间就是一个独立的网络协议栈,它有自己的网络接口,路由,以及防火墙规则. 网络命名空间通常是位于目录/var/run/netns/下的文件描述符. 例如,使用ip netns add命令创建一个命名空间: ip netns add bule 查看目录/var/run/netns/: $ ls /var/run/netns/ blue 网络命名空间常用于虚拟化中.因

How to Use PHP Namespaces, Part 1: The Basics

命名空间是一个重要的概念.该系列文章详细介绍了PHP对命名空间的支持及用法.原文地址:http://www.sitepoint.com/php-53-namespaces-basics/ 命名空间是PHP 5.3诸多重要更新中的一个.它会使C#和Java开发者感到友好,同时很有希望使PHP应用的程序结构变得更好. 为什么需要命名空间? 随着你的PHP代码库的增长,意外重定义之前已声明过的函数的风险也在增加.这个问题会在引入第三方组件或插件时恶化——如果多段代码都实现了”Database”或者”U

Linux Namespaces机制

Linux Namespaces机制提供一种资源隔离方案.PID,IPC,Network等系统资源不再是全局性的,而是属于特定的Namespace.每个Namespace里面的资源对其他Namespace都是透明的.要创建新的Namespace,只需要在调用clone时指定相应的flag.Linux Namespaces机制为实现基于容器的虚拟化技术提供了很好的基础,LXC(Linux containers)就是利用这一特性实现了资源的隔离.不同container内的进程属于不同的Namespa

XML 命名空间(XML Namespaces)

XML 命名空间提供避免元素命名冲突的方法. 命名冲突 在 XML 中,元素名称是由开发者定义的,当两个不同的文档使用相同的元素名时,就会发生命名冲突. 这个 XML 文档携带着某个表格中的信息: <table> <tr> <td>Apples</td> <td>Bananas</td> </tr> </table> 这个 XML 文档携带有关桌子的信息(一件家具): <table> <nam

NAMESPACE_ERR: An attempt is made to create or change an object in a way which is incorrect with regard to namespaces.

解决办法: http://stackoverflow.com/questions/4037125/namespace-err-an-attempt-is-made-to-create-or-change-an-object-in-a-way-which-i http://docs.spring.io/spring-ws/site/faq.html#namespace_err I get NAMESPACE_ERR exceptions when using Spring-WS. What can