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代码所创建的专门负责编写约束的代码.为我们简化了许多代码量. 二.使用步骤 使用步骤 ...
随机推荐
- WD-保修验证(WCC7K4ARTDF1)
https://support.wdc.com/warranty/warrantystatus.aspx?lang=cn WCC7K4ARTDF1 有限保修期限内 WD40EFRX WD Red 09 ...
- ArcEngine数据编辑--选择要素
转自原文ArcEngine数据编辑--选择要素 好久没有写博文了,这段时间相对空闲一点,把AE数据编辑实现总结下. 要编辑要素,首先要选中要素,按shift键进行多选,按esc键清空选择. 个人了解的 ...
- css3-3 css3背景样式
css3-3 css3背景样式 一.总结 一句话总结:网站页面上的小图标集成在一张大图上面,是因为降低服务器负载,网站上的那些图片都可以下载下来,源码那里,或者工具那里. 1.background:# ...
- AngularJS之watch
简介 首先apply方法会触发evel方法,当evel方法解析成功后,会去触发digest方法,digest方法会触发watch方法. 在digest执行时,如果watch观察的的value与上一次执 ...
- 卡特兰(Catalan)数列
卡特兰数又称卡塔兰数,英文名 Catalan number,是组合数学中一个常出现在各种计数问题中出现的数列.以比利时的数学家欧仁·查理·卡塔兰 (1814–1894)的名字来命名,其前几项为 : 1 ...
- 【t082】牛跑步
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 新牛到部队,CG要求它们每天早上搞晨跑,从A农场跑到B农场.从A农场到B农场中有n-2个路口,分别标上 ...
- [NPM] Run npm scripts in series
After creating several npm script it becomes useful to run multiple scripts back-to-back in series. ...
- tensorflow 的版本差异与变化
官方文档的说明,Transitioning to TensorFlow 1.0 1. 简单列举如下 变量 tf.VARIABLES ⇒ tf.GLOBAL_VARIABLES tf.all_varia ...
- 【codeforces 776A】A Serial Killer
[题目链接]:http://codeforces.com/contest/776/problem/A [题意] 这个杀手每天会除掉一个人; 这个杀手每天都有两个目标; 给你它杀人的日志,以及这个人被杀 ...
- android tips—开机引导启动wifi设置
在开机引导(Setupwizard,Guide)中都有关于wifi设置项,我得做法例如以下: Intent intent = new Intent(); intent.setClassName(&qu ...