码迷,mamicode.com
首页 > 其他好文 > 详细

Namespaces

时间:2014-10-22 21:40:57      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   ar   for   sp   div   on   cti   

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 }

 

Namespaces

标签:style   blog   color   ar   for   sp   div   on   cti   

原文地址:http://www.cnblogs.com/yoghourt/p/4044226.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!