惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Security Latest
Security Latest
Jina AI
Jina AI
爱范儿
爱范儿
Attack and Defense Labs
Attack and Defense Labs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
GbyAI
GbyAI
N
News | PayPal Newsroom
Last Week in AI
Last Week in AI
V
Visual Studio Blog
SecWiki News
SecWiki News
H
Heimdal Security Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
腾讯CDC
L
LINUX DO - 热门话题
I
InfoQ
罗磊的独立博客
博客园_首页
Know Your Adversary
Know Your Adversary
T
The Exploit Database - CXSecurity.com
博客园 - 【当耐特】
阮一峰的网络日志
阮一峰的网络日志
Hacker News - Newest:
Hacker News - Newest: "LLM"
T
Threat Research - Cisco Blogs
美团技术团队
IT之家
IT之家
AWS News Blog
AWS News Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
N
Netflix TechBlog - Medium
Help Net Security
Help Net Security
S
Secure Thoughts
Microsoft Security Blog
Microsoft Security Blog
Schneier on Security
Schneier on Security
Spread Privacy
Spread Privacy
B
Blog RSS Feed
N
News and Events Feed by Topic
L
Lohrmann on Cybersecurity
Google Online Security Blog
Google Online Security Blog
Microsoft Azure Blog
Microsoft Azure Blog
N
News and Events Feed by Topic
I
Intezer
Blog — PlanetScale
Blog — PlanetScale
云风的 BLOG
云风的 BLOG
A
Arctic Wolf
C
Cyber Attacks, Cyber Crime and Cyber Security
G
Google Developers Blog
The Register - Security
The Register - Security
The GitHub Blog
The GitHub Blog
MyScale Blog
MyScale Blog
F
Full Disclosure

博客园 - Kevin Li

使用WMI远程部署/更新BizTalk程序集 基于WS-AtomicTransaction标准的WCF远程分布式事务(补充) 基于WS-AtomicTransaction标准的WCF远程分布式事务(二) 基于WS-AtomicTransaction标准的WCF远程分布式事务(一) 在Jeffrey Zhao的基础上+反编译System.Web.Extensions.Design得到的完整Ajax代码 .net remoting的事务传播以及wcf分布式事务 在多线程环境下使用HttpWebRequest或者调用Web Service 一篇介绍.NET 2.0 范型挺全面的文章 Windows 2003 下分布式事务协调器(DTC)的配置 使用SQLSourceSafe控制数据库的版本 MySql 主键(自动增加)的数据类型所带来的错误 使用TraceListener 自动保存跟踪信息到文件中 接口属性集成与智能提示的奇怪问题 动态创建程序集中的类 动态创建程序集中的类 使用NUnit测试包含应用程序配置的dll文件 解决“COM+ 无法与 Microsoft 分布式事务协调程序交谈”的问题 [导入]一些很有用的Reflector 插件 Web安全性原则
C# 2.0中的范型和Nullable范型
Kevin Li · 2005-05-28 · via 博客园 - Kevin Li

Posted on 2005-05-28 22:38  Kevin Li  阅读(1854)  评论()    收藏  举报

在.net 1.1中我们无法对基础类型,如int、DateTime、enum类型等赋予null的值,但这在实际应用中往往会带来很多麻烦,比如有下面这个函数:

class OrderDAC{
    public ArrayList GetOrders(OrderStatus status){
    }
}
public enum OrderStatus{
    Initial,
    Canceled,
    Delivered
}
如果我们希望该函数GetOrders中当输入的为status == null 的时候获取所有OrderStatus的订单,而如果status不等于 null 时获取指定状态下的订单,在.net 1.1中根本无法实现,因为我们无法将 status 设为 null,因为它是基础类型。

又考虑另外一种情况,如:

OrderInfo order = new OrderInfo();
order.DeliveryDate = reader.IsDbNull("DeliveryDate") == true? null : (DateTime) reader["DeliveryDate"];

上面的代码实际上无法编译通过,因为DateTime不允许付空,取而代之的是:
if( reader.IsDbNull("DeliveryDate") == false){
    order.DeliveryDate = (DateTime) reader["DeliveryDate"];
}

当我们要判断 order.DeliveryDate 是否有赋值,也不能采用 if( order.DeliveryDate == null ),这时往往需要给类似的字段赋一个默认值,比如 DateTime.MinValue,但这处理起来也是挺麻烦的。

幸运的是,在.net 2.0中,提供了 Nullable的范型,通过它,我们可以为基础类型如int等赋予null的值,具体如下:

class OrderDAC{
    public List<OrderInfo> GetOrders(Nullable<OrderStatus> status){
    }
}
上面用到了两个范型,一个是List<OrderInfo>,一个是Nullable<OrderStatus>,有了这个,我们就可以调用
OrderDAC ord = new OrderDAC();
List<OrderInfo> orders = ord.GetOrders(null);

这样我们就可以为 OrderStatus status 赋予null的值,是不是方便了很多呢?

同样,我们也可以定义
class OrderInfo{
    public Nullable<DateTime> DeliveryDate;
}

这样,我们就可以为OrderInfo.DeliveryDate 赋予 null,并且可以判断 OrderInfo.DeliveryDate == null ,又方便了很多。

其实象这样的例子还有很多:)

.net 2.0 确实为我们提供了很多便利,期待正式版!!