C# 自定义泛型类,并添加约束
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GenericExam
{
class Program
{
static void Main(string[] args)
{
var dm = new DocumentManager<Document>();
dm.AddDocument(new Document("Title A","Content A"));
dm.AddDocument(new Document("Title B","Content B"));
dm.DisplayAllDocument();
if (dm.IsDocumentAvailable)
{
Document d = dm.GetDocument();
Console.WriteLine(d.Title);
}
Console.Read();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GenericExam
{
public interface IDocument
{
string Title { get; set; }
string Content { get; set; }
}
public class Document:IDocument
{
public string Title { get; set; }
public string Content { get; set; }
public Document(string title, string content)
{
this.Title = title;
this.Content = content;
}
}
public class DocumentManager<T> where T : IDocument
{
public Queue<T> DocumentQueue = new Queue<T>();
public void AddDocument(T doc)
{
lock(this)
{
DocumentQueue.Enqueue(doc);
}
}
public bool IsDocumentAvailable
{
get { return DocumentQueue.Count > 0; }
}
public T GetDocument()
{
T doc = default(T);
lock (this)
{
doc = DocumentQueue.Dequeue();
}
return doc;
}
public void DisplayAllDocument()
{
foreach (T doc in DocumentQueue)
{
Console.WriteLine(((IDocument)doc).Title);
}
}
}
}
C# 自定义泛型类,并添加约束的更多相关文章
- iOS - XIB之AutoLayout添加约束
XIB--AutoLayout添加约束 仿QQ登录界面: 说明:以下各图背景红色只是方便看清楚: 1.创建工程:创建xib文件 2.打开xib文件: (1).创建头像: 拖控件:uiimageview ...
- uiscrollView UINavigation和uitabbar添加约束的问题
首先是层次问题, 我的storyboard中 底层是一个View(viewcontroller自带)上面添加了一个UIScrollview(添加约束, 上下左右全为0), 在UIScrollview上 ...
- 使用SQL语句向已有数据表添加约束
如果向存在数据的表里添加约束,有可能会出现数据不符合检查约束而造成添加约束失败. 如: 这是一个表,为身份证号添加检查约束. USE DEmo--指向当前操作的数据库 GO ALTER TABLE E ...
- 5-06使用Sql 语句为表添加约束
约束名的命名规则推荐采用:约束类型_约束列. 为用户表添加约束 ALTER TABLE UserInfo ADD CONSTRALNT PK_UserId PRIMATY REY(UserId) CO ...
- 每日学习心得:SharePoint 2013 自定义列表项添加Callout菜单项、文档关注、SharePoint服务端对象模型查询
前言: 前一段时间一直都比较忙,没有什么时间进行总结,刚好节前项目上线,同时趁着放假可以好好的对之前遇到的一些问题进行总结.主要内容有使用SharePoint服务端对象模型进行查询.为SharePoi ...
- olacle数据库员工表的创建,及插入数据,添加约束,删除列,查询数据的sql语句
---删除原有的员工表drop TABLE employee;---创建员工表CREATE TABLE employee ( empno NUMBER(4) NOT NULL, ...
- iOS开发小技巧--UIScrollView内部子控件添加约束的注意点
注意:用UIScrollView时布局子控件的时候,不要相对于UIScrollView来添加约束,这样做不是设置子控件的位置,反而是设置了UIScrollView的contentSize 子控件的尺寸 ...
- 怎样使用AutoLayOut为UIScrollView添加约束
1.在ViewController中拖入1个UIScrollView,并为其添加约束 约束为上下左右四边与superview对齐 2.在scrollview中,拖入1个UIView,为了便于区分将其设 ...
- SQL语句创建数据库,SQL语句删除数据库,SQL语句创建表,SQL语句删除表,SQL语句添加约束,SQL语句删除约束
创建数据库: CREATE DATABASE Test --要创建的数据库名称 ON PRIMARY ( --数据库文件的具体描述 NAME='Test_data', --主数据文件的逻辑名称 FIL ...
- Autolayout-VFL语言添加约束
一.VFL语言简洁 VFL(Visual format language)语言是苹果为了简化手写Autolayout代码所创建的专门负责编写约束的代码.为我们简化了许多代码量. 二.使用步骤 使用步骤 ...
随机推荐
- 【p081】ISBN号码
Time Limit: 1 second Memory Limit: 50 MB [问题描述] 每一本正式出版的图书都有一个ISBN号码与之对应,ISBN码包括9位数字.1位识别码和3位分隔符,其规定 ...
- HDU 5072 Coprime (单色三角形+容斥原理)
题目链接:Coprime pid=5072"> 题面: Coprime Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
- [SCSS] Organize SCSS into Multiple Files with Partials
Tired of dealing with monolithic CSS files? Are requests for multiple CSS files hurting your perform ...
- [Flow] The Fundamentals of Flow
Install: yarn global add flow-typed /*get type defination*/ yarn add flow-bin -D For example you hav ...
- js进阶 10-3 jquery中为什么用document.ready方法
js进阶 10-3 jquery中为什么用document.ready方法 一.总结 一句话总结: 1.document.ready和window.onload的区别:用哪个好? document. ...
- Watchdog机制概述
1. Watchdog初始 Watchdog的中文的“看门狗”,有保护的意思.最早引入Watchdog是在单片机系统中,由于单片机的工作环境容易受到外界磁场的干扰,导致程序“跑飞”,造成整个系统无法正 ...
- 百度UEditor上传图片-再再总结一次
本周,CSDN有个网友遇到了百度UEditor上传问题,最后商定付50元钱,我帮他解决这个问题. 他最初想自己搞定这个问题,结果搞了好多次,好几天,还是没能解决. 2015年1月17日8: ...
- css中背景 字体 文体属性练习
@charset "utf-8"; body{ background-color:#332244;/*//背景颜色*/ background-image:("../kk. ...
- 怎么样Windows7在配置ASPserverIIS
在百度经验浏览:http://jingyan.baidu.com/article/5553fa82ed97c765a23934f3.html Internet Information Services ...
- Android菜鸟的成长笔记(24)——Android中的振动器
在某些时候,程序需要启动系统振动器,比如手机静音时使用振动提示用户:再比如玩游戏时,当系统碰撞.爆炸时使用振动带给用户更逼真的体验等.总之,振动是除视频.声音之外的另一种"多媒体" ...