积极答复者
一个语法问题 没见过 希望可以讲解下

问题
答案
-
这是C#的语法糖。其实这个语法在CLR编译器中被转化为:
Nullable<DateTime>。是一个值类型,可空类型。
表示这个东西可以赋值为null。
你或许感到奇怪——为何值类型Nullable<DateTime>可以赋值null?
比如:
Nullable<DateTime> dt = null; //其实被编译器理解为: Nullable<DateTime> dt = new Nullable<DateTime>(); //但是注意源码: [Serializable, StructLayout(LayoutKind.Sequential), TypeDependency("System.Collections.Generic.NullableEqualityComparer`1"), TypeDependency("System.Collections.Generic.NullableComparer`1")] public struct Nullable<T> where T: struct { private bool hasValue; internal T value; public Nullable(T value) { this.value = value; this.hasValue = true; } public bool HasValue { get { return this.hasValue; } } public T Value { get { if (!this.HasValue) { ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_NoValue); } return this.value; } } 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; } } 注意:值类型默认就有空构造函数,因此你不必显示定义! 因此空构造函数其实把hasValue定义为空了。所以本质上Nullable<T>还是保存了数值的,
只是人为用HasValue屏蔽了。If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats- 已建议为答案 Jason Dot WangModerator 2013年4月16日 8:49
- 已标记为答案 Jason Dot WangModerator 2013年4月23日 9:12
全部回复
-
这是C#的语法糖。其实这个语法在CLR编译器中被转化为:
Nullable<DateTime>。是一个值类型,可空类型。
表示这个东西可以赋值为null。
你或许感到奇怪——为何值类型Nullable<DateTime>可以赋值null?
比如:
Nullable<DateTime> dt = null; //其实被编译器理解为: Nullable<DateTime> dt = new Nullable<DateTime>(); //但是注意源码: [Serializable, StructLayout(LayoutKind.Sequential), TypeDependency("System.Collections.Generic.NullableEqualityComparer`1"), TypeDependency("System.Collections.Generic.NullableComparer`1")] public struct Nullable<T> where T: struct { private bool hasValue; internal T value; public Nullable(T value) { this.value = value; this.hasValue = true; } public bool HasValue { get { return this.hasValue; } } public T Value { get { if (!this.HasValue) { ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_NoValue); } return this.value; } } 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; } } 注意:值类型默认就有空构造函数,因此你不必显示定义! 因此空构造函数其实把hasValue定义为空了。所以本质上Nullable<T>还是保存了数值的,
只是人为用HasValue屏蔽了。If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats- 已建议为答案 Jason Dot WangModerator 2013年4月16日 8:49
- 已标记为答案 Jason Dot WangModerator 2013年4月23日 9:12
-
hi,
this 语句若出现在方法签章,这表示他是一个扩充方法,请参考以下做法
http://www.dotblogs.com.tw/yc421206/archive/2009/03/29/7755.aspxDateTime? 表示要让基础类型支援null
http://msdn.microsoft.com/zh-cn/library/2cf62fcy.aspx
秘訣無它,唯勤而已 http://www.dotblogs.com.tw/yc421206/
- 已编辑 余小章MVP 2013年4月16日 10:14
-
小章,可能這個客戶就是要對可空的DateTime進行拓展,所以他做的並沒有錯;)
另外針對這個意見,我提出相反的結論——根據反編譯結果,我不認為需要這個額外的擴展:
public override string ToString() { if (!this.HasValue) { return ""; } return this.value.ToString(); }
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats -
hello ProgrammingVolunteer,
你误会了,我并不是说他有错,而是说他是一个扩展方法
秘訣無它,唯勤而已 http://www.dotblogs.com.tw/yc421206/
O(∩_∩)O~
既然是扩展方法,貌似是允许(this)存在的;)
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats -
使用反编译工具呗,免费可以用Just-Decompiler,IL Spy等。
收费(免费可以使用14天啥的:Reflector)。
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats