Chinaunix

标题: C# 名字空间 [打印本页]

作者: 失的猛    时间: 2011-12-21 08:41
标题: C# 名字空间

C#语法之名字空间

名字空间在内部和外部都是组织应用程序的好办法,很容易组织层次关系

名字空间默认类型internal.

为了结构化地管理程序,经常使用名字空间,名字空间的声明如下:

Namespace 空间

{

名字空间

}

 

在名字空间中,可以声明一个或多个:另一个名字空间、类、接口、结构、枚举、代表元.

 

  1. /**
  2.  * 引用PresentationFramework/System/WindowsBase;
  3.  * 代码文件:stackApp.cs
  4.  */
  5. //Stack 名字空间

  6. namespace Stack
  7. {
  8.     using System;
  9.     public class Stack
  10.     {
  11.         //first:栈最上面一个节点

  12.         private Node first = null;

  13.         //count:栈中结点的数量

  14.         private int count = 0;

  15.         //判空属性,提供get访问器

  16.         public bool Empty
  17.         {
  18.             get
  19.             {
  20.                 return (first == null);
  21.             }
  22.         }

  23.         //计数属性,提供get访问器

  24.         public int Count
  25.         {
  26.             get
  27.             {
  28.                 return count;
  29.             }
  30.         }

  31.         //压栈操作,返回注意object

  32.         public object Pop()
  33.         {
  34.             if (first == null)
  35.             {
  36.                 throw new InvalidOperationException("Can't pop from an empty stack.");
  37.             }
  38.             else
  39.             {
  40.                 object temp = first.Value;
  41.                 first = first.Next;
  42.                 count--;
  43.                 return temp;
  44.             }
  45.         }

  46.         //弹栈操作,返回空

  47.         public void Push(object o)
  48.         {
  49.             first = new Node(o, first);
  50.             count++;
  51.         }

  52.         //结点类

  53.         class Node
  54.         {
  55.             //结点具有两个属性:自己、指向下一个结点

  56.             public Node Next;
  57.             public object Value;

  58.             public Node(object value) : this(value, null) { }

  59.             public Node(object value, Node next)
  60.             {
  61.                 Next = next;
  62.                 Value = value;
  63.             }
  64.         }

  65.         class StackApp
  66.         {
  67.             static void Main()
  68.             {
  69.                 Stack s = new Stack();

  70.                 if (s.Empty)
  71.                     Console.WriteLine("This is an empty Stack.");
  72.                 else
  73.                     Console.WriteLine("This is not an empty Stack.");

  74.                 //往栈中压入5个结点

  75.                 for (int i = 0; i < 5; ++i)
  76.                     s.Push(i);

  77.                 Console.WriteLine("往栈中压入了{0}个元素", s.Count);

  78.                 //把栈中结点全部弹出来

  79.                 for (int i = 0; i < 5; ++i)
  80.                     Console.WriteLine("弹出了第{0}个元素,还剩{1}个元素.", (int)s.Pop() + 1, s.Count);

  81.                 s = null;
  82.                 System.Windows.Application app = new System.Windows.Application();
  83.                 app.Run();
  84.             }
  85.         }
  86.     }
  87. }

参考来源:Visual C#时尚编程百例 






欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2