C#学习之泛型
//主函数//主函数里面调用的类都在后面有具体描述
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace GeneralType
{
class Program
{
static void Main(string[] args)
{
LinkedListNode<int> sub = new LinkedListNode<int>();
LinkedListNode<int> sub1 = new LinkedListNode<int>();
LinkedListNode<int> sub2 = new LinkedListNode<int>();
sub.next = sub1;
sub.prev = null;
sub1.prev = sub;
sub1.next = sub2;
sub2.prev = sub1;
sub2.next = null;
LinkedListNode<int> point;
point = sub2;
while(point!=null)
{
string text= point.Value.ToString();
Console.WriteLine(text);
point = point.prev;
}
Console.ReadLine();
//上面是创建节点类的实例,下面是创建了一个链表的实例
var link = new LinkList<int>();
link.Addlast();
link.Addlast();
link.Addlast();
link.Addlast();
foreach(int i in link)
{
Console.WriteLine(i);
}
Console.ReadLine();
return ;
} }
}
//以下是建立了两个类,分别在不同的文件中//LinkedListNode<T>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace GeneralType
{
public class LinkedListNode<T>
{
//创建节点类,对象可以使全部类型
//本类具有值Value 的属性
//值需要在初始化的时候加入,不能再类外部通过其他方式对
//本类一个对象的值赋值
//本类包含两个引用属性,一个是指向本节点在链表中前面的节点,一个是指向后面的节点
//可以在本类中使用
//Internal修饰符是指在本程序集内可以赋值
public LinkedListNode(T value)
{
this.Value=value;
}
public T Value
{
private set;
get;
}
public LinkedListNode<T> prev
{
get;
internal set;
}
public LinkedListNode<T> next
{
get;
internal set;
}
}
}
//LinkList<T>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections; namespace GeneralType
{
public class LinkList<T>:IEnumerable<T>
{
//首先先为本类创建两个属性,First and Last
public LinkedListNode<T> First { get; private set; }
public LinkedListNode<T> Last { get; private set; }
//下面的函数式在向建立的链表对象中添加数据
//同时返回这个数据所建立的新的节点对象
public LinkedListNode<T> Addlast(T one)
{
var newnode = new LinkedListNode<T>(one);
if(First==null)
{
First = newnode;
Last = newnode;
First.next = Last;
Last.prev = First;
}
else
{
Last.next = newnode;
newnode.prev = Last;
Last = newnode;
}
return newnode;
}
//通过实现GetEnumerable()方法可以使用foreach 遍历链表
//yield return和yield break 可以多次出现,但是不能用return语句
public IEnumerator<T> GetEnumerator()
{
LinkedListNode<T> current = this.First;
while(current!=null)
{
yield return current.Value;
//Console.WriteLine(current.ToString());
current = current.next;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}
C#学习之泛型的更多相关文章
- C#学习之泛型继承和静态成员
想要理解这里有必要先将泛型类学习充分.这里讲解的是泛型类继承类的类型和静态成员. 在前面C#学习之泛型中,创建的LinkList<T>类实现了IEnumerable<T>接口. ...
- Java 理论和实践: 了解泛型 识别和避免学习使用泛型过程中的陷阱
Brian Goetz (brian@quiotix.com), 首席顾问, Quiotix 简介: JDK 5.0 中增加的泛型类型,是 Java 语言中类型安全的一次重要改进.但是,对于初次使用泛 ...
- Java编程思想学习(十一) 泛型
1.概要 generics enable types (classes and interfaces) to be parameters when defining classes, interfac ...
- Java学习之——泛型
1.概要 generics enable types (classes and interfaces) to be parameters when defining classes, interfac ...
- ios开发ios9新特性关键字学习:泛型,逆变,协变,__kindof
一:如何去学习?都去学习什么? 1:学习优秀项目的设计思想,多问几个为什么,为什么要这么设计,这么设计的好处是什么,还能不能在优化 ,如何应用到自己的项目中 2:学习优秀项目的代码风格,代码的封装设计 ...
- luogg_java学习_09_泛型_集合
这篇博客总结了半天,希望自己以后返回来看的时候理解更深刻,也希望可以起到帮助初学者的作用. 转载请注明 出自 : luogg的博客园 , 泛型 泛型介绍 1).类内部的属性的类型可以由外部决定: 2) ...
- [.NET自我学习]Delegate 泛型
阅读导航 委托Delegate 泛型 1. 委托Delegate 继承自MulticastDelegate 声明委托定义签名: public delegate int DemoDelegate(int ...
- .net学习之泛型、程序集和反射
一.泛型1.CLR编译时,编译器只为MyList<T>类型产生“泛型版”的IL代码——并不进行泛型的实例化,T在中间只充当占位符.例如:MyList 类型元数据中显示的<T> ...
- 【java基础学习】泛型
泛型 1. 泛型类(声明的泛型类型静态方法不能使用) class Tools<T>{ private T t; public void set(T t){ this.t = t; } pu ...
- 学习Swift -- 泛型
泛型 泛型代码可以让你写出根据自我需求定义.适用于任何类型的,灵活且可重用的函数和类型.它的可以让你避免重复的代码,用一种清晰和抽象的方式来表达代码的意图. 泛型所解决的问题 先来看一个交换两个int ...
随机推荐
- 拓扑排序 (Ordering Tasks UVA - 10305)
题目描述: 原题:https://vjudge.net/problem/UVA-10305 题目思路: 1.依旧是DFS 2.用邻接矩阵实现图 3.需要判断是否有环 AC代码 #include < ...
- [译] JavaScript核心指南(JavaScript Core) 【转】
本文转自:http://remember2015.info/blog/?p=141#scope-chain 零.索引 对象(An Object) 原型链(A Prototype Chain) 构造函数 ...
- 【第三章】Shell 变量的数值计算
一.算数运算符 shell中常见的算术运算符: shell中常见的算术命令: 1. 整数运算 方法一:expr expr命令就既可以用于整数运算,也可以用于相关字符串长度.匹配等的运算处理: exp ...
- C++clock()延时循环
函数clock(),返回程序开始执行后所用的系统时间,但是有两个复制问题. 1.clock()返回时间的单位不一定是秒 2.该函数的返回类型在某些系统上可能是Long,也可能是unsigned lon ...
- vim常用命令—撤销与反撤销
命令模式下(即按ESC后的模式) u 撤销 Ctrl r (组合键) 反撤销<后悔撤销>
- UVa 1586 - Molar Mass - ACM/ICPC Seoul 2007 - C语言
关键在于判断数字是两位数还是单位数,其他部分没有难度. #include"stdio.h" #include"string.h" #include"c ...
- Bacon's Cipher(培根密码)
Description Bacon's cipher or the Baconian cipher is a method of steganography (a method of hiding a ...
- Alpha发布——Thunder团队
视频展示 视频链接: 爱奇艺: http://www.iqiyi.com/w_19ruzwru25.html (画质清晰,但可能需多次刷新或重新打开页面,此问题因电脑型号和网络而异) 优酷: ...
- CSS基础小记
2017/10/29 CSS 认识CSS样式 CSS全称为"层叠样式表 (Cascading Style Sheets)",它主要是用于定义HTML内容在浏览器内的显示样式,如文字 ...
- iOS如何做出炫酷的翻页效果
详情链接http://www.jianshu.com/p/b6dc2595cc3e https://github.com/schneiderandre/popping