欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#泛型用法实例分析

程序员文章站 2023-11-13 00:00:58
本文实例分析了c#泛型用法。分享给大家供大家参考。具体分析如下: 这里演示如何创建具有单个类型参数的自定义泛型列表类,以及如何实现 ienumerable

本文实例分析了c#泛型用法。分享给大家供大家参考。具体分析如下:

这里演示如何创建具有单个类型参数的自定义泛型列表类,以及如何实现 ienumerable<t> 以便对列表的内容启用 foreach 迭代。此示例还演示客户端代码如何通过指定类型参数来创建该类的实例,以及该类型参数的约束如何实现对类型参数执行其他操作。

using system;
using system.collections;
using system.collections.generic;
using system.text;
namespace generics_csharp
{
  // 尖括号中的类型参数 t。
  public class mylist<t> : ienumerable<t>
  {
    protected node head;
    protected node current = null;
    // 嵌套类型也是 t 上的泛型
    protected class node
    {
      public node next;
      // t 作为私有成员数据类型。
      private t data;
      // 在非泛型构造函数中使用的 t。
      public node(t t)
      {
        next = null;
        data = t;
      }
      public node next
      {
        get { return next; }
        set { next = value; }
      }
      // t 作为属性的返回类型。
      public t data
      {
        get { return data; }
        set { data = value; }
      }
    }
    public mylist()
    {
      head = null;
    }
    // t 作为方法参数类型。
    public void addhead(t t)
    {
      node n = new node(t);
      n.next = head;
      head = n;
    }
    // 实现 getenumerator 以返回 ienumerator<t>,从而启用列表的
    // foreach 迭代。请注意,在 c# 2.0 中, 
    // 不需要实现 current 和 movenext。
    // 编译器将创建实现 ienumerator<t> 的类。
    public ienumerator<t> getenumerator()
    {
      node current = head;
      while (current != null)
      {
        yield return current.data;
        current = current.next;
      }
    }
    // 必须实现此方法,因为
    // ienumerable<t> 继承 ienumerable
    ienumerator ienumerable.getenumerator()
    {
      return getenumerator();
    }
  }
  public class sortedlist<t> : mylist<t> where t : icomparable<t>
  {
    // 一个未优化的简单排序算法,
    // 该算法从低到高对列表元素排序:
    public void bubblesort()
    {
      if (null == head || null == head.next)
        return;
      bool swapped;
      do
      {
        node previous = null;
        node current = head;
        swapped = false;
        while (current.next != null)
        {
          // 由于需要调用此方法,因此,sortedlist
          // 类在 ienumerable<t> 上是受约束的
          if (current.data.compareto(current.next.data) > 0)
          {
            node tmp = current.next;
            current.next = current.next.next;
            tmp.next = current;
            if (previous == null)
            {
              head = tmp;
            }
            else
            {
              previous.next = tmp;
            }
            previous = tmp;
            swapped = true;
          }
          else
          {
            previous = current;
            current = current.next;
          }
        }// end while
      } while (swapped);
    }
  }
  // 一个将自身作为类型参数来实现 icomparable<t> 的简单类,
  // 是对象中的
  // 常用设计模式,这些对象
  // 存储在泛型列表中。
  public class person : icomparable<person>
  {
    string name;
    int age;
    public person(string s, int i)
    {
      name = s;
      age = i;
    }
    // 这会使列表元素
    // 按 age 值排序。
    public int compareto(person p)
    {
      return age - p.age;
    }
    public override string tostring()
    {
      return name + ":" + age;
    }
    // 必须实现 equals。
    public bool equals(person p)
    {
      return (this.age == p.age);
    }
  }
  class generics
  {
    static void main(string[] args)
    {
      // 声明并实例化一个新的范型 sortedlist 类。
      // person 是类型参数。
      sortedlist<person> list = new sortedlist<person>();
      // 创建 name 和 age 值以初始化 person 对象。
      string[] names = new string[] { "franscoise", "bill", "li", "sandra", "gunnar", "alok", "hiroyuki", "maria", "alessandro", "raul" };
      int[] ages = new int[] { 45, 19, 28, 23, 18, 9, 108, 72, 30, 35 };
      // 填充列表。
      for (int x = 0; x < names.length; x++)
      {
        list.addhead(new person(names[x], ages[x]));
      }
      console.writeline("unsorted list:");
      // 打印出未排序的列表。
      foreach (person p in list)
      {
        console.writeline(p.tostring());
      }
      // 对列表进行排序。
      list.bubblesort();
      console.writeline(string.format("{0}sorted list:", environment.newline));
      // 打印出排序的列表。
      foreach (person p in list)
      {
        console.writeline(p.tostring());
      }
      console.writeline("done");
    }
  }
}

希望本文所述对大家的c#程序设计有所帮助。