C# 類型定義


1、定義枚舉(enum)
2、定義隊列(Queue)
3、定義堆棧(stack)
4、
定義ArrayList
5、定義HashTable
6、定義StortedList
7、
定義接口


定義枚舉(enum)
    //枚举简单来说就是用户定义的一个数据类型,其中包含有限的数据,用一个易记的名称表示,它具有使程序代码清晰,易读,使代码更易于键入的优点
    
//用enum來定義枚舉
    
//emun 枚舉名: 枚舉類型
    enum orientation : byte
    {
        north 
= 1,
        south 
= 2,
        east 
= 3,
        west 
= 4
    }

  
public class Ch05Ex02
    {
        
//定義枚舉變量並賦值
        orientation myDriecton = orientation.north;
    }

定義隊列(Queue)
            using System.Collections;

            
//Queue(隊列)類實現了一個先入先出(FiFO)機制,元素將在隊列的尾部插入(信隊操作),並從隊列的頭部移除
            Queue numbers = new Queue();

            
//填充隊列
            foreach (int iNumber in new int[4] { 9372 })
            {
                
//Enqueue:將物件加入 Queue 的末端。 
                numbers.Enqueue(iNumber);
                Console.WriteLine(iNumber 
+ "has joined the queue");
            }
            
//遍歷隊列
            foreach (int iNumber in numbers)
            {
                Console.WriteLine(iNumber);
            }
            
//清空隊列
            while (numbers.Count != 0)
            {
                
//Dequeue:移除並傳回在 Queue 前端的物件。
                int number = (int)numbers.Dequeue();
                Console.WriteLine(number 
+ " has left the queue");
            }

定義堆棧(stack)
            using System.Collections;

            
//Stack()堆棧類實現了一個後入先出(LIFO)機制。元素在堆的頂部進入堆棧(push),也從頂部離開堆棧(pop)
            Stack numbers = new Stack();
            
//填充堆棧
            foreach (int number in new int[4] { 9,3,7,2})
            {
                
//Push:將物件插入 Stack 的頂端。
                numbers.Push(number);
                Console.WriteLine(number
+" has been pushed on the stack");
            }
            
//遍歷堆棧
            foreach (int number in numbers)
            {
                Console.WriteLine(number);
            }
            
//清空堆棧
            while (numbers.Count!=0)
            {
                
//Pop:移除並傳回在 Stack 頂端的物件。 
                
//Peek :傳回 Stack 頂端的物件而不需移除它。 
                int number = (int)numbers.Pop();
                Console.WriteLine(number
+" has been popped off the stack");
            }

定義ArrayList
ArrayList主要用於對一個數組中的元素進行各種處理

            using System.Collections;

            ArrayList alNum = new ArrayList();
            
//填充ArrayList
            foreach (int number in new int[12]{10,9,8,7,7,6,5,10,4,3,2,1})
            {
                
//Add()將物件加入 ArrayList 的末端。 
                alNum.Add(number);
                
//Insert():將元素插入 ArrayList 中的指定索引處。 
            }
            
//Remove():從ArrayList 移除特定物件之第一個符合的元素。
            
//移除值為7的第一個元素(也就是第4個元素,索引3)
            alNum.Remove(7);
            
//RemoveAt():移除 ArrayList 的指定索引處的元素。 
            
//移除當前的第7個元素,即索引6(值10)
            alNum.RemoveAt(6);
            
//使用For語句遍歷剩余的10個元素
            for (int i = 0; i < alNum.Count; i++)
            {
                
int number = (int)alNum[i];
                Console.WriteLine(number);
            }
            
//使用foreach語句遍歷剩余的10個元素
            foreach (int number in alNum)
            {
                Console.WriteLine(number);
            }


定義HashTable
數組ArrayList都提供了一種方式將一個整數索引映射到一個元。你需要對方括號中提供整數索引(比如[4]),並獲得位於索引4處的元素(實是第5個元素) 。但在某此情況下,可以希望提供一個特殊的映射,作為映射來源的類型不是一個int,而是其他某個類型比如string,double等。在其它語言中通常稱為關聯數組(associativearray),Hashtabl(哈希表)類提供這種功能。

            using System.Collections;

            Hashtable htAges 
= new Hashtable();
            
//填充Hashtable
            htAges["John"= 41;
            htAges[
"Diana"= 42;
            htAges[
"James"= 13;
            htAges[
"Francesca"= 11;
            //填充值方法2
            //htAges.Add("John",39);


            
//使用一個foreach語句來遍歷
            
//迭代器生成一個DictionaryEntry對象,其中含一個鍵/值對
            
//DictionaryEntry:定義可設定或擷取的字典索引鍵/值組配對。
            foreach (DictionaryEntry element in htAges)
            {
                
string name = (string)element.Key;
                
int age = (int)element.Value;
                Console.WriteLine(
"Name: {0}, Age {1}",name,age); 
            }
            
//輸出指定Key的Value
            Console.WriteLine(htAges["John"].ToString());

定義StortedList

SortedList類與Hashtable類非常相似,兩者都充許將Key與value關聯起。它們主要區別在於,在SortedList中Keys數據總是排好序的。

            using System.Collections;

            SortedList slAges 
= new SortedList();
            slAges[
"John"= 39;
            slAges[
"Diana"= 40;
            slAges[
"James"= 12;
            slAges[
"Francesca"= 10;
            
foreach (DictionaryEntry element in slAges)
            {
                
string name = (string)element.Key;
                
int age = (int)element.Value;
                Console.WriteLine(
"Name: {0}, Age: {1}",name,age);
            }
            
//輸出指定Key的Value
            Console.WriteLine(slAges["John"].ToString());

定義接口
    //定義接口
    interface IScreenPosition
    {
        
int x
        {
            
get;
            
set;
        }
        
int y
        {
            
get;
            
set;
        }
    }
    
//在結構中實現結口
    struct ScreenPositon:IScreenPosition
    {
        
public int x
        {
            
get { }
            
set { }
        }
        
public int y
        {
            
get { }
            
set { }
        }
    }
    
//在類中實現口
    class ScreenPositon:IScreenPosition
    {
        
public virtual int x
        {
            
get { }
            
set { }
        }
        
public virtual int y
        {
            
get { }
            
set { }
        }
        
//注:virtual關鍵字在struct中是效的,因為能從struct中派生;struct是隱式sealed(密封)的
    }




原文地址:https://www.cnblogs.com/scottckt/p/904223.html