首页 > 代码库 > 类型安全---泛型与非泛型
类型安全---泛型与非泛型
============================================================非泛型
-------------------------LinkedListNode.cs类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { public class LinkedListNode { //上一个对象 //下一个对象 //值 public LinkedListNode Prev { get; set; } public LinkedListNode Next { get; set; } public object Value { get; private set; } public LinkedListNode(object o) { this.Value = o; } } }
-------------------------LinkedList.cs类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace ConsoleApplication1
{
public class LinkedList:IEnumerable
{
//头部
//尾部
public LinkedListNode First { get; set; }
public LinkedListNode Last { get; set; }
public LinkedListNode AddValue(object o)
{
LinkedListNode lln = new LinkedListNode(o);
if (First == null)
{
First = lln;
Last = lln;
}
else
{
LinkedListNode buffer = Last;//缓存上一个对象
Last.Next = lln;//设置上一个对象的Next(指定上一个对象的下一个对象是lln)
Last = lln;//设置尾部对象为lln
Last.Prev = buffer;//设置lln的上一个对象
}
return lln;
}
public IEnumerator GetEnumerator()
{
LinkedListNode lln = First;
while (lln != null)
{
yield return lln.Value;
lln = lln.Next;
}
}
}
}
-------------------------主方法
static void Main(string[] args) { LinkedList ll = new LinkedList(); ll.AddValue("asdas"); ll.AddValue("sad"); ll.AddValue("asdsdaas"); ll.AddValue("asddsadaasdasdasas"); foreach (object o in ll) { Console.WriteLine(o); Console.ReadKey(); } }
缺点:不断的装箱拆箱,数据类型不安全,代码性能差
============================================================泛型
-------------------------LinkListNode.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { public class LinkListNode<T> { //上一对象 //下一对象 //值 public LinkListNode<T> Prev { get; set; } public LinkListNode<T> Next { get; set; } public T Value { get; private set; } public LinkListNode(T value) { this.Value = value; } } }
-------------------------LinkList.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; namespace ConsoleApplication1 { public class LinkList<T>:IEnumerable<T> { //头部 //尾部 public LinkListNode<T> First { get; set; } public LinkListNode<T> Last { get; set; } public LinkListNode<T> AddList(T value) { LinkListNode<T> lln = new LinkListNode<T>(value); if (First == null) { First = lln; Last = lln; } else { LinkListNode<T> buffer = Last; Last.Next = lln; Last = lln; Last.Prev = buffer; } return lln; } public IEnumerator<T> GetEnumerator() { LinkListNode<T> lln = First; while (lln != null) { yield return lln.Value; lln = lln.Next; } } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } } }
-------------------------主方法
static void Main(string[] args) { var i =new LinkList<int>(); i.AddList(1); i.AddList(2); foreach (int l in i) { Console.WriteLine(l); Console.ReadKey(); } }
本文出自 “程序猿的家” 博客,请务必保留此出处http://962410314.blog.51cto.com/7563109/1438125