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 }

 

posted on 2014-10-22 20:29  yoghourt  阅读(136)  评论(0编辑  收藏  举报