Nullable<T> 的实现
2011-12-09 12:43
288 查看
[Serializable, StructLayout(LayoutKind.Sequential), TypeDependency("System.Collections.Generic.NullableComparer`1"), TypeDependency("System.Collections.Generic.NullableEqualityComparer`1")] public struct Nullable<T> where T: struct { private bool hasValue; internal T value; [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")] public Nullable(T value) { this.value = value; this.hasValue = true; } public bool HasValue { get { return this.hasValue; } } public T Value { [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")] get { if (!this.HasValue) { ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_NoValue); } return this.value; } } [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")] public T GetValueOrDefault() { return this.value; } public T GetValueOrDefault(T defaultValue) { if (!this.HasValue) { return defaultValue; } return this.value; } public override bool Equals(object other) { if (!this.HasValue) { return (other == null); } if (other == null) { return false; } return this.value.Equals(other); } public override int GetHashCode() { if (!this.HasValue) { return 0; } return this.value.GetHashCode(); } public override string ToString() { if (!this.HasValue) { return ""; } return this.value.ToString(); } public static implicit operator T?(T value) { return new T?(value); } public static explicit operator T(T? value) { return value.Value; } }
相关文章推荐
- List<> 转换为Dataset的C#代码实现 解决Nullable问题
- <Android>greenrobot-EventBus,guava-Event Bus的异步实现
- <asp.net> 实现返回功能
- JavaScript实现回车键切换输入框焦点<转>
- 自己动手,实现一种类似List<T>的数据结构(二)
- 模拟实现通讯录<二>(动态模拟)
- <Unity UGUI> 在Unity5 的UGUI系统上实现图片的遮罩
- <7> Android HAL hardware.c f分析 Android帧缓冲区(Frame Buffer)硬件抽象层(HAL)模块Gralloc的实现原理分析
- 如何实现具有层次结构的 TreeView <四> (WPF/TreeView/Style/Template)
- C# LinkedList<T> 泛型类的实现
- 第九周试验任务1--定义Time类中的<<和>>运算符的重载,实现输入和输出。
- AJAX跨域方法之一—使用<script>实现
- 用<<,>>,|,&实现一个字word(ABCD)的高低位转换
- System.Nullable<T> int? 数据库中的int类型可以为空 在C# 中 的处理
- share中简单聊天界面的实现<代码>
- <Win32 API> 钩子程序的实现
- <Linux+Qt>使用Qt实现单例模式
- Box2d源码学习<四>数学库API的实现
- (礼拜一log)前端开发:<td> 标签的 colspan 属性 & 两个div并排、一个宽度固定的实现方法
- 如何实现批处理的连续调用?<一星>