1.创建泛型 类-C#高级编程
2016-12-10 16:37
337 查看
本文固定连接:http://blog.csdn.net/u013108312/article/details/53559438
namespace Wrox.ProCSharp.Generics { public class LinkedListNode { public LinkedListNode(object value) { this.Value = value; } public object Value { get; private set; } public LinkedListNode Next { get; internal set; } public LinkedListNode Prev { get; internal set; } } }
using System.Collections; namespace Wrox.ProCSharp.Generics { public class LinkedList : IEnumerable { public LinkedListNode First { get; private set; } public LinkedListNode Last { get; private set; } public LinkedListNode AddLast(object node) { var newNode = new LinkedListNode(node); if (First == null) { First = newNode; Last = First; } else { Last.Next = newNode; Last = newNode; } return newNode; } public IEnumerator GetEnumerator() { LinkedListNode current = First; while (current != null) { yield return current.Value; current = current.Next; } } } }
using System; namespace Wrox.ProCSharp.Generics { class Program { static void Main() { var list1 = new LinkedList(); list1.AddLast(2); list1.AddLast(4); list1.AddLast("6"); foreach (int i in list1) { Console.WriteLine(i); } } } }
namespace Wrox.ProCSharp.Generics { public class LinkedListNode<T> { public LinkedListNode(T value) { this.Value = value; } public T Value { get; private set; } public LinkedListNode<T> Next { get; internal set; } public LinkedListNode<T> Prev { get; internal set; } } }
using System.Collections; using System.Collections.Generic; namespace Wrox.ProCSharp.Generics { public class LinkedList<T> : IEnumerable<T> { public LinkedListNode<T> First { get; private set; } public LinkedListNode<T> Last { get; private set; } public LinkedListNode<T> AddLast(T node) { var newNode = new LinkedListNode<T>(node); if (First == null) { First = newNode; Last = First; } else { Last.Next = newNode; Last = newNode; } return newNode; } public IEnumerator<T> GetEnumerator() { LinkedListNode<T> current = First; while (current != null) { yield return current.Value; current = current.Next; } } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } } }
using System; namespace Wrox.ProCSharp.Generics { class Program { static void Main() { var list2 = new LinkedList<int>(); list2.AddLast(1); list2.AddLast(3); list2.AddLast(5); foreach (int i in list2) { Console.WriteLine(i); } var list3 = new LinkedList<string>(); list3.AddLast("2"); list3.AddLast("four"); list3.AddLast("foo"); foreach (string s in list3) { Console.WriteLine(s); } } } }
相关文章推荐
- C#高级编程三十天----泛型结构,泛型方法,泛型托付
- C# 高级编程 泛型
- 如何:为泛型列表创建迭代器块(C# 编程指南)
- C#高级编程笔记 Day 5, 2016年9月 13日 (泛型)
- C#泛型编程指导原则(一)
- C#泛型编程指导原则(六、七)
- 九、C#编程实战演习ABC 3、创建C# 类库 (Dll)
- c# 高级编程学习(二)
- Wrox的C#高级编程第三版第一部分第一章(22~23页)
- C#2005 .NET3.0高级编程学习笔记————命名空间
- 泛型接口(C# 编程指南) ---From MSDN
- Wrox的C#高级编程第三版第一部分第一章(18~21页)
- C#2005 .NET3.0高级编程学习笔记————类和结构,类的数据成员,类的函数成员(方法、属性)
- C#2005 .NET3.0高级编程学习笔记———.NET体系结构
- C#2005 .NET3.0高级编程学习笔记———C#基础
- C#泛型编程指导原则(十、十一)
- 九、C#编程实战演习ABC 2、用Visual C# 创建Windows应用程序
- Wrox的C#高级编程第三版第一部分第一章(16~18页)
- C# 高级编程 - .NET结构
- 使用ADO.NET和C#以编程方式创建 SQL Server 数据库