Util application Framework Common operations Class (10): Nullable value type extension

Source: Internet
Author: User

When you use a nullable value type, you find it inconvenient to take a value, such as a GUID? obj, you want to get the value from obj, you can use the Value property, obj. Value, but obj may be null, and an exception is thrown.

A nullable value type provides a HasValue property that identifies if obj is not a null value, and you need to add this to determine the if (value) whenever you get a nullable value. HasValue) {var value = obj. Value;}.

Let's wrap up the judgment by several extension methods.

Add extensions.nullable. cs to the Util project, as shown in the code below.

usingSystem;namespaceUtil {/// <summary>    ///Extended-Nullable type/// </summary>     Public Static Partial classExtensions {/// <summary>        ///Safe return value/// </summary>        /// <param name= "value" >Nullable Values</param>         Public Static intSafevalue ( This int?value) {            returnValue. HasValue? Value. Value:0; }        /// <summary>        ///Safe return value/// </summary>        /// <param name= "value" >Nullable Values</param>         Public StaticGuid Safevalue ( ThisGuid?value) {            returnValue. HasValue?value.        Value:Guid.Empty; }        /// <summary>        ///safe return value, NULL return Datetime.minvalue/// </summary>        /// <param name= "value" >Nullable Values</param>         Public StaticDateTime Safevalue ( ThisDatetime?value) {            returnValue. HasValue?value.        Value:DateTime.MinValue; }        /// <summary>        ///Safe return value/// </summary>        /// <param name= "value" >Nullable Values</param>         Public Static BOOLSafevalue ( This BOOL?value) {            returnValue. HasValue &&value.        Value; }        /// <summary>        ///Safe return value/// </summary>        /// <param name= "value" >Nullable Values</param>         Public Static DoubleSafevalue ( This Double?value) {            returnValue. HasValue? Value. Value:0; }        /// <summary>        ///Safe return value/// </summary>        /// <param name= "value" >Nullable Values</param>         Public Static decimalSafevalue ( This decimal?value) {            returnValue. HasValue? Value. Value:0; }    }}

In the extensions directory of the Util.tests test project, add the Nullableextensiontest code as follows.

usingSystem;usingMicrosoft.VisualStudio.TestTools.UnitTesting;namespaceUtil.Tests.Extensions {/// <summary>    ///Nullable type extension/// </summary>[TestClass] Public classNullableextensiontest {/// <summary>        ///Test Nullable integral type/// </summary>[TestMethod] Public voidTestsafevalue_int () {int? Value =NULL; Assert.AreEqual (0, value.            Safevalue ()); Value=1; Assert.AreEqual (1, value.        Safevalue ()); }        /// <summary>        ///to test for a nullable GUID/// </summary>[TestMethod] Public voidTestsafevalue_guid () {Guid? Value =NULL; Assert.AreEqual (Guid.Empty, value.            Safevalue ()); Value=Guid.NewGuid (); Assert.AreEqual (value. Value, value.        Safevalue ()); }        /// <summary>        ///Test Nullable datetime/// </summary>[TestMethod] Public voidTestsafevalue_datetime () {DateTime? Value =NULL; Assert.AreEqual (Datetime.minvalue, value.            Safevalue ()); Value= Conv.todate ("2000-1-1" ); Assert.AreEqual (value. Value, value.        Safevalue ()); }        /// <summary>        ///Test nullable bool/// </summary>[TestMethod] Public voidTestsafevalue_boolean () {BOOL? Value =NULL; Assert.AreEqual (false, value.            Safevalue ()); Value=true; Assert.AreEqual (true, value.        Safevalue ()); }        /// <summary>        ///Test Nullable Double/// </summary>[TestMethod] Public voidtestsafevalue_double () {Double? Value =NULL; Assert.AreEqual (0, value.            Safevalue ()); Value=1.1; Assert.AreEqual (1.1, value.        Safevalue ()); }        /// <summary>        ///Test Nullable decimal/// </summary>[TestMethod] Public voidTestsafevalue_decimal () {decimal? Value =NULL; Assert.AreEqual (0, value.            Safevalue ()); Value=1.1M; Assert.AreEqual (1.1M, value.        Safevalue ()); }    }}

In the future, the nullable value type can be safely obtained by Safevalue method.

. NET Application Framework Exchange QQ Group: 386092459, welcome interested friends to join the discussion.

Thank you for your continued attention, my blog address: http://www.cnblogs.com/xiadao521/

: Http://files.cnblogs.com/xiadao521/Util.2015.1.6.1.rar

Util application Framework Common operations Class (10): Nullable value type extension

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.