惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
SegmentFault 最新的问题
博客园 - 三生石上(FineUI控件)
爱范儿
爱范儿
博客园 - 聂微东
V
Visual Studio Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
M
MIT News - Artificial intelligence
The GitHub Blog
The GitHub Blog
Recent Announcements
Recent Announcements
有赞技术团队
有赞技术团队
L
LangChain Blog
I
InfoQ
T
Tailwind CSS Blog
博客园 - 【当耐特】
V
V2EX
博客园_首页
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
GbyAI
GbyAI
Vercel News
Vercel News
雷峰网
雷峰网
量子位
A
About on SuperTechFans
Martin Fowler
Martin Fowler
H
Help Net Security

博客园 - Mat

VBScript Functions and Keyword - Mat Windows Management Interface (WMI) 用div布局时怎么让文字或图片垂直居中对齐呢? CSS控制的内容超过容器宽度后显示省略号 div内容可编辑 Access C#从视频截图的方法 异步工作流体系结构的选择 笔记 vs2005中的WebBrowser控件的简单应用 CSS 布局 如何在 ASP.NET 应用程序中实现模拟 再谈css--如何针对不同位置的元素使用不同的风格 给web用户控件自定义后台事件 Hashtable Dictionary 《冰鉴》—— 《金刚经》 《道子》——大智慧 排序算法
.Net C# 集合
Mat · 2006-09-03 · via 博客园 - Mat

      集合提供了一种将任意对象格式化存储的方法,一个集合可以被定义为一个实现了一个或多个System.Collections.ICollection、System.Collections.IDictionary和System.Collections.IList的对象。以下是一些常用的.Net内部的集合:

  • System.Collections.ArrayList
  • System.Collections.Stack
  • System.Collections.Queue
  • System.Collections.HashTable

      ArrayList

      当需要大小可按需动态增加,并希望通过索引来进行访问的数组的时候,使用ArrayList

   ArrayList myAL = new ArrayList();
   myAL.Add("Hello");
   myAL.Add("World");
   myAL.Add("!");

   Console.WriteLine(myAL[0] + myAL[1] + myAL[2]

      Stack

      当需要一个能实现后进先出的集合时,使用Stack

  

// Creates and initializes a new Stack.
   Stack myStack = new Stack();
   myStack.Push("Hello");
   myStack.Push("World");
   myStack.Push("!");

// Displays the properties and values of the Stack.
   Console.WriteLine( "myStack" );
   Console.WriteLine( "\tCount:    {0}", myStack.Count );
   Console.Write( "\tValues:" );

   System.Collections.IEnumerator myEnumerator = myStack.GetEnumerator();
   while ( myEnumerator.MoveNext() )
      Console.Write( "\t{0}", myEnumerator.Current);
   Console.WriteLine();


      Queue

      当需要一个先进先出的集合时,使用Queue

  

// Creates and initializes a new Queue.
   Queue myQ = new Queue();
   myQ.Enqueue("Hello");
   myQ.Enqueue("World");
   myQ.Enqueue("!");

// Displays the properties and values of the Queue.
   Console.WriteLine( "myQ" );
   Console.WriteLine( "\tCount:    {0}", myQ.Count );
   Console.Write( "\tValues:" );

      Hashtable

      当需要一个可以按Key值来查询的数组是,使用Hashtable

  

// Creates and initializes a new Hashtable.
   Hashtable myHT = new Hashtable();
   myHT.Add("First", "Hello");
   myHT.Add("Second", "World");
   myHT.Add("Third", "!");

// Displays the properties and values of the Hashtable.
   Console.WriteLine( "myHT" );
   Console.WriteLine( "\tCount:    {0}", myHT.Count );
   Console.WriteLine( "\t Values:" );
   
   IDictionaryEnumerator myEnumerator = myHT.GetEnumerator();
   Console.WriteLine( "\t-KEY-\t-VALUE-" );
   while ( myEnumerator.MoveNext() )
    Console.WriteLine("\t{0}:\t{1}", myEnumerator.Key, myEnumerator.Value);
   Console.WriteLine();

       当然,除了.Net自定义的这些集合外,也可以通过实现IList,ICollection,IEnumerable来定义自己集合类,如:DataTable.Rows就是实现ICollection,IEnumerable接口。但这样的工作比较繁琐,需要实现的方法很多,还要考虑存储和索引,所以通常的做法是直接继承自ArrayList , Stack , Queue , HashTable,进一步封装,如:

 public class Users:ArrayList 
 {
    public Users()
    {
    }

public new User this[int index]
    {
       get
       {
            return (User)(base[index]);
       }
       set
       {
            base[index] = value;
       }
    }

public int Add(User value)
    {
       return base.Add(value);
    }
 }

 public class User
 {
  public User()
  {
   //
   // TODO: Add constructor logic here
   //
  }
  private string m_strName;
  private int m_intAge;
  private string m_strEmail;
  public string Name
  {
   get
   {
    return this.m_strName;
   }
   set
   {
    this.m_strName = value;
   }
  }
  public int Age
  {
   get
   {
    return this.m_intAge;
   }
   set
   {
    this.m_intAge  = value;
   }
  }
  public string Eamil
  {
   get
   {
    return this.m_strEmail;
   }
   set
   {
    this.m_strEmail = value;
   }
  }
 }

这样既增加了可读性,实现起来也容易,使用起来也方便,可以直接使用objUsers[0].Name来访问