當你使用可空的值類型時,你會發現取值很不方便,好比Guid? obj,你要從obj中獲取值,能夠使用Value屬性obj. Value,但obj可能爲null,這時候就會拋出一個異常。安全
可空值類型提供了一個HasValue屬性,它能夠識別出obj是否是一個null值,每當你獲取可空值都須要加上這個判斷if(value.HasValue){ var value = obj.Value;}。框架
下面咱們經過幾個擴展方法,把判斷封裝起來。測試
在Util項目中添加Extensions.Nullable.cs,代碼以下。ui
using System; namespace Util { /// <summary>
/// 擴展 - 可空類型 /// </summary>
public static partial class Extensions { /// <summary>
/// 安全返回值 /// </summary>
/// <param name="value">可空值</param>
public static int SafeValue( this int? value ) { return value.HasValue ? value.Value : 0; } /// <summary>
/// 安全返回值 /// </summary>
/// <param name="value">可空值</param>
public static Guid SafeValue( this Guid? value ) { return value.HasValue ? value.Value : Guid.Empty; } /// <summary>
/// 安全返回值,爲null返回DateTime.MinValue /// </summary>
/// <param name="value">可空值</param>
public static DateTime SafeValue( this DateTime? value ) { return value.HasValue ? value.Value : DateTime.MinValue; } /// <summary>
/// 安全返回值 /// </summary>
/// <param name="value">可空值</param>
public static bool SafeValue( this bool? value ) { return value.HasValue && value.Value; } /// <summary>
/// 安全返回值 /// </summary>
/// <param name="value">可空值</param>
public static double SafeValue( this double? value ) { return value.HasValue ? value.Value : 0; } /// <summary>
/// 安全返回值 /// </summary>
/// <param name="value">可空值</param>
public static decimal SafeValue( this decimal? value ) { return value.HasValue ? value.Value : 0; } } }
在Util.Tests測試項目的Extensions目錄中,添加NullableExtensionTest,代碼以下。this
using System; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace Util.Tests.Extensions { /// <summary>
/// 可空類型擴展 /// </summary>
[TestClass] public class NullableExtensionTest { /// <summary>
/// 測試可空整型 /// </summary>
[TestMethod] public void TestSafeValue_Int() { int? value = null; Assert.AreEqual( 0, value.SafeValue() ); value = 1; Assert.AreEqual( 1, value.SafeValue() ); } /// <summary>
/// 測試可空Guid /// </summary>
[TestMethod] public void TestSafeValue_Guid() { Guid? value = null; Assert.AreEqual( Guid.Empty, value.SafeValue() ); value = Guid.NewGuid(); Assert.AreEqual( value.Value, value.SafeValue() ); } /// <summary>
/// 測試可空DateTime /// </summary>
[TestMethod] public void TestSafeValue_DateTime() { DateTime? value = null; Assert.AreEqual( DateTime.MinValue, value.SafeValue() ); value = Conv.ToDate( "2000-1-1" ); Assert.AreEqual( value.Value, value.SafeValue() ); } /// <summary>
/// 測試可空bool /// </summary>
[TestMethod] public void TestSafeValue_Boolean() { bool? value = null; Assert.AreEqual( false, value.SafeValue() ); value = true; Assert.AreEqual( true, value.SafeValue() ); } /// <summary>
/// 測試可空double /// </summary>
[TestMethod] public void TestSafeValue_Double() { double? value = null; Assert.AreEqual( 0, value.SafeValue() ); value = 1.1; Assert.AreEqual( 1.1, value.SafeValue() ); } /// <summary>
/// 測試可空decimal /// </summary>
[TestMethod] public void TestSafeValue_Decimal() { decimal? value = null; Assert.AreEqual( 0, value.SafeValue() ); value = 1.1M; Assert.AreEqual( 1.1M, value.SafeValue() ); } } }
之後經過SafeValue方法就能夠安全的獲取可空值類型了。spa
calvinK同窗給出一個更好的方案,代碼以下,在此表示感謝。code
namespace Util { /// <summary> /// 擴展 - 可空類型 /// </summary> public static partial class Extensions { /// <summary> /// 安全返回值 /// </summary> /// <param name="value">可空值</param> public static T SafeValue<T>( this T? value ) where T : struct { return value ?? default( T ); } } }
.Net應用程序框架交流QQ羣: 386092459,歡迎有興趣的朋友加入討論。blog
謝謝你們的持續關注,個人博客地址:http://www.cnblogs.com/xiadao521/ci
下載地址:http://files.cnblogs.com/xiadao521/Util.2015.1.6.1.rarget