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

推荐订阅源

MyScale Blog
MyScale Blog
T
The Blog of Author Tim Ferriss
博客园 - 三生石上(FineUI控件)
博客园_首页
WordPress大学
WordPress大学
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
The GitHub Blog
The GitHub Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
F
Fortinet All Blogs
B
Blog RSS Feed
Stack Overflow Blog
Stack Overflow Blog
Microsoft Security Blog
Microsoft Security Blog
雷峰网
雷峰网
GbyAI
GbyAI
M
MIT News - Artificial intelligence
I
InfoQ
阮一峰的网络日志
阮一峰的网络日志
Recent Announcements
Recent Announcements
小众软件
小众软件
量子位
S
SegmentFault 最新的问题
Google DeepMind News
Google DeepMind News
Vercel News
Vercel News
aimingoo的专栏
aimingoo的专栏

博客园 - bengxia

DataGridView中的{索引-1沒有值}异常 Silverlight + WCF + Sharepoint(Silverlight Web Part) 问题集合 CLR Via C# 3rd 阅读摘要 -- Chapter 29 – Hybrid Thread Synchronization Constructs CLR Via C# 3rd 阅读摘要 -- Chapter 28 – Primitive Thread Synchronization Constructs FIX: Unable to find a version of the runtime to run this application. CLR Via C# 3rd 阅读摘要 -- Chapter 27 – I/O-Bound Asynchronous Operations CLR Via C# 3rd 阅读摘要 -- Chapter 26 – Compute-Bound Asynchronous Operations CLR Via C# 3rd 阅读摘要 -- Chapter 25 – Thread Basics CLR Via C# 3rd 阅读摘要 -- Chapter 24 – Runtime Serialization CLR Via C# 3rd 阅读摘要 -- Chapter 23 – Assembly Loading and Reflection CLR Via C# 3rd 阅读摘要 -- Chapter 22 – CLR Hosting and AppDomains CLR Via C# 3rd 阅读摘要 -- Chapter 21 – Automatic Memory Management (Garbage Collection) CLR Via C# 3rd 阅读摘要 -- Chapter 20 – Exceptions and State Management - bengxia CLR Via C# 3rd 阅读摘要 -- Chapter 18 - Custom Attributes CLR Via C# 3rd 阅读摘要 -- Chapter 17 - Delegates CLR Via C# 3rd 阅读摘要 -- Chapter 16 - Arrays - bengxia CLR Via C# 3rd 阅读摘要 -- Chapter 15 - Enumerated Types and Bit Flags CLR Via C# 3rd 阅读摘要 -- Chapter 14 - Chars, Strings, and Working with Text CLR Via C# 3rd 阅读摘要 -- Chapter 13 - Interfaces
CLR Via C# 3rd 阅读摘要 -- Chapter 19 – Nullable Value Types
bengxia · 2010-04-23 · via 博客园 - bengxia

C#'s Support for Nullable Value Types

  1. 原来C#中的值类型是不可以 = null的,现在如果定义Int32? x = null;类型后面价格?就可以了;
  2. 实际上是多了一个类型:public struct Nullable<T> where T : struct;
  3. Int32? 在语义上等同于Nullable<Int32>;
  4. C#的操作符对null的处理;
  5. 为了管理nullable的实例,生成了一大堆代码。

C#'s Null-Coalescing Operator

  1. null-coalescing操作符(??)。Int32? x = b ?? 123。如果b = null,那么 x = 123,否则 x = b;
  2. ??可以很好的用在组合情况下。

The CLR Has Special Support for Nullable Value Types

  1. 装箱Nullable的值类型,当装箱Nullable的值类型时,会检查如果为null,啥也不做返回null,否则装箱;
  2. 拆箱Nullable的值类型,如果引用为null设置值为null,否则拆箱;
  3. 在Nullable值类型上调用GetType,会用实际类型代替Nullable返回;
  4. 在Nullable值类型上调用接口方法,虽然Nullable没有实现,但是如果值类型实现了接口就可以调用。

本章小结

   本章讲了如何使用Nullable的值类型,为什么Nullabe<T>?其中一个理由就是与数据库内容表示的一致性。首先讲了如何使用Nullable的值类型,然后讲了??操作符,最后讲了CLR对Nullable提供了哪些特别的支持。