C#集合类用法实例代码详解

 更新时间:2017年10月23日 14:12:33   投稿:mrr  
本文通过实例代码给大家介绍了C#集合类用法的相关知识,非常不错,具有参考借鉴价值,需要的朋友可以参考下

下面介绍C#的集合类

1ArrayList

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace 动态数组ArrayList
{
  class Program
  {
    static void Main(string[] args)
    {
      ArrayList a1 = new ArrayList();
      a1.Add(100);
      foreach (int number in new int[6] { 9, 3, 7, 2, 4, 8 })
      {
        a1.Add(number);
      }
      int[] number2 = new int[2] { 11, 12 };
      a1.AddRange(number2);
      a1.Remove(3);
      a1.RemoveAt(3);
      ArrayList al2 = new ArrayList(a1.GetRange(1,3));
      Console.WriteLine("遍历方法1:");
      foreach (int i in a1)
      {
        Console.WriteLine(i);
      }
      Console.WriteLine("遍历方法2:");
      for (int i = 0; i < al2.Count; i++)
      {
        Console.WriteLine(al2[i]);
      }
      Console.ReadLine();
    }
  }
}

2 Stack

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace Stack集合类
{
  class Program
  {
    static void Main(string[] args)
    {
      Stack s1 = new Stack();
      Stack s2 = new Stack();
      foreach (int i in new int[4] { 1, 2, 3, 4 })
      {
        s1.Push(i);
        s2.Push(i);
      }
      foreach (int i in s1)
      {
        Console.WriteLine(i);
      }
      s1.Pop();
      Console.WriteLine("出栈");
      foreach (int i in s1)
      {
        Console.WriteLine(i);
      }
      int num=(int)s2.Peek();
      Console.WriteLine("弹出最后一项{0}",num);
      foreach (int i in s2)
      {
        Console.WriteLine(i);
      }
      Console.ReadLine();
    }
  }
}

3Queue

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Collections;
namespace Queue集合类
{
  class Program
  {
    static void Main(string[] args)
    {
      Queue q1 = new Queue();
      Queue q2 = new Queue();
      foreach(int i in new int [4]{1,2,3,4})
      {
        q1.Enqueue(i);
        q2.Enqueue(i);
      }
      foreach (int i in q1)
      {
        Console.WriteLine(i);
      }
      q1.Dequeue();
      Console.WriteLine("q1出队");
      foreach (int i in q1)
      {
        Console.WriteLine(i);
      }
      int num=(int)q2.Peek();
      Console.WriteLine("取q2开始处{0}",num);
      foreach(int i in q2)
      {
        Console.WriteLine(i);
      }
      Console.ReadLine();
    }
  }
}

4Hashtable

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace Hashtable集合类
{
  class Program
  {
    static void Main(string[] args)
    {
      Hashtable h = new Hashtable();
      h.Add("E","e");
      h.Add("B", "b");
      h.Add("C", "c");
      h.Add("A", "a");
      foreach (DictionaryEntry e in h)
      {
        Console.Write("{0},{1} ", e.Key, e.Value);
      }
      Console.WriteLine();
      string s = (string)h["C"];
      Console.WriteLine(s);
      if (h.Contains("E"))
      {
        Console.WriteLine("含有E");
      }
      Console.WriteLine(h["A"]);
      h.Remove(h["A"]);
      h.Clear();
      foreach (DictionaryEntry e in h)
      {
        Console.Write("{0},{1} ", e.Key, e.Value);
      }
      Console.ReadLine();
    }
  }
}

5SortedList

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Collections;
namespace SortedList集合类
{
  class Program
  {
    static void Main(string[] args)
    {
      SortedList s1 = new SortedList();
      s1["c"]=41;
      s1["a"]=42;
      s1["d"]=11;
      s1["b"]=13;
      foreach (DictionaryEntry e in s1)
      {
        string s = (string)e.Key;
        int i = (int)e.Value;
        Console.Write("{0},{1} ",s,i);
      }
      Console.ReadLine();
    }
  }
}

总结

以上所述是小编给大家介绍的C#集合类用法实例代码详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

相关文章

  • C#搜索文字在文件及文件夹中出现位置的方法

    C#搜索文字在文件及文件夹中出现位置的方法

    这篇文章主要介绍了C#搜索文字在文件及文件夹中出现位置的方法,涉及C#针对文件及文件夹遍历与查找的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-08-08
  • Base64编码解码原理及C#编程实例

    Base64编码解码原理及C#编程实例

    这篇文章主要介绍了Base64编码解码原理及C#编程实例,本文讲解了Base64编码由来、Base64编码原理、C#编程实现,需要的朋友可以参考下
    2014-10-10
  • 详解c#读取XML的实例代码

    详解c#读取XML的实例代码

    XML文件是一种常用的文件格式,本篇文章主要介绍了c#读取XML的实例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-12-12
  • C#实现的文件批量重命名功能示例

    C#实现的文件批量重命名功能示例

    这篇文章主要介绍了C#实现的文件批量重命名功能,结合具体实例形式分析了C#针对文件的遍历、属性修改相关操作技巧,需要的朋友可以参考下
    2017-07-07
  • C#获取所有进程的方法

    C#获取所有进程的方法

    在本篇文章里小编给大家分享了关于C#获取所有进程的方法和步骤,有需要的朋友们跟着学习参考下。
    2018-12-12
  • C#操作excel打印的示例

    C#操作excel打印的示例

    这篇文章主要介绍了C#操作excel打印的示例,帮助大家利用c#打印表格,提高办公效率,感兴趣的朋友可以了解下
    2020-10-10
  • C#实现递归算法经典实例

    C#实现递归算法经典实例

    这篇文章主要为大家介绍了C#实现递归算法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-01-01
  • C#迭代器方法介绍

    C#迭代器方法介绍

    这篇文章主要介绍了C#迭代器方法,可以使用foreach循环语句进行的迭代的方法,称为可迭代方法,或者迭代器方法,方法操作,想了解更多内容得小伙伴可以学习下面文章内容,希望给你的学习带来帮助
    2022-03-03
  • C# Winform 实现TCP发消息

    C# Winform 实现TCP发消息

    这篇文章主要介绍了C# Winform 实现TCP发消息的示例,帮助大家更好的理解和学习使用c#技术,感兴趣的朋友可以了解下
    2021-03-03
  • C#定义并实现单链表实例解析

    C#定义并实现单链表实例解析

    这篇文章主要介绍了C#定义并实现单链表实例解析,有助于读者加深对C#实现数据结构的理解,需要的朋友可以参考下
    2014-07-07

最新评论