可空类型的使用

可空类型也是值类型,但是它是包含null值的值类型,可以像 int? nullable = null 来表示可空类型,在C#中实际上是没有int?这种类型的,对于编译器而言,int?会被编译成Nullable<int>类型,即可空类型。

让我们来看看Nullable<int>中常见的方法属性:

1.public bool HasValue { get; }

获取一个值,指示当前的 System.Nullable<T> 对象是否有值;如果当前的 System.Nullable<T> 对象具有值,则为 true;如果当前的System.Nullable<T>对象没有值,则为false。

实例演示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Generic
{
    class Program
    {
        static void Main(string[] args)
        {
            Nullable<int> nullValue = null;
            Nullable<int> notNullValue = 1;
            //HasValue属性指示可空对象是否有值
            Console.WriteLine("nullValue是否为空:{0}", nullValue.HasValue);
            Console.WriteLine("notNullValue是否为空:{0}", notNullValue.HasValue);

            Console.ReadKey();

        }
    }
}

打印结果:

2.public T Value { get; }
获取当前的 System.Nullable<T> 值,如果 System.Nullable<T>.HasValue 属性为 true,则为当前 System.Nullable<T> 对象的值;如果System.Nullable<T>.HasValue 属性为 false,则将引发异常。异常: System.InvalidOperationException:System.Nullable<T>.HasValue 属性为 false。

实例演示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Generic
{
    class Program
    {
        static void Main(string[] args)
        {
            Nullable<int> nullValue = null;
            Nullable<int> notNullValue = 1;
            if(nullValue.HasValue)
            {
                Console.WriteLine("值为:{0}", nullValue.Value);
            }
            if(notNullValue.HasValue)
            {
                Console.WriteLine("值为:{0}", notNullValue.Value);
            }

            Console.ReadKey();

        }
    }
}

打印结果:

3.public override bool Equals(object other);
指示当前 System.Nullable<T> 对象是否等于指定的对象。

实例演示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Generic
{
    class Program
    {
        static void Main(string[] args)
        {
            Nullable<int> nullValue = null;
            Nullable<int> notNullValue = 1;
            if(nullValue.Equals(notNullValue))
            {
                Console.WriteLine("两个值相等");
            }
            else
            {
                Console.WriteLine("两个值不相等");
            }

            Console.ReadKey();

        }
    }
}

打印结果:

4.public T GetValueOrDefault();
检索当前 System.Nullable<T> 对象的值,或该对象的默认值,;如果 System.Nullable<T>.HasValue 属性为 true,则为 System.Nullable<T>.Value 属性的值;否则为当前ystem.Nullable<T> 对象的默认值。 默认值的类型为当前 System.Nullable<T> 对象的类型参数,而默认值的值中只包含二进制零。

实例演示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Generic
{
    class Program
    {
        static void Main(string[] args)
        {
            Nullable<int> nullValue = null;
            Nullable<int> notNullValue = 1;
            if(nullValue.HasValue)
            {
                Console.WriteLine("实际值为:{0}", nullValue);
            }
            else
            {
                Console.WriteLine("默认值为:{0}", nullValue.GetValueOrDefault());
            }
            if(notNullValue.HasValue)
            {
                Console.WriteLine("实际值为:{0}", notNullValue);
            }
            else
            {
                Console.WriteLine("默认值为{0}", notNullValue.GetValueOrDefault());
            }

            Console.ReadKey();

        }
    }
}

打印结果:

5.public T GetValueOrDefault(T defaultValue);
检索当前 System.Nullable<T> 对象的值或指定的默认值;defaultValue:如果 System.Nullable<T>.HasValue 属性为 false,则为一个返回值。返回结果: 如果 System.Nullable<T>.HasValue 属性为 true,则为 System.Nullable<T>.Value 属性的值;否则为
defaultValue 参数。

实例演示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Generic
{
    class Program
    {
        static void Main(string[] args)
        {
            Nullable<int> nullValue = null;
            Nullable<int> notNullValue = 1;
            if(nullValue.HasValue)
            {
                Console.WriteLine("实际值为:{0}", nullValue);
            }
            else
            {
                Console.WriteLine("默认值为:{0}", nullValue.GetValueOrDefault(-1));
            }
            if(notNullValue.HasValue)
            {
                Console.WriteLine("实际值为:{0}", notNullValue);
            }
            else
            {
                Console.WriteLine("默认值为{0}", notNullValue.GetValueOrDefault(-2));
            }

            Console.ReadKey();

        }
    }
}

打印结果:

6.public override string ToString();
返回当前 System.Nullable<T> 对象的值的文本表示形式;如果 System.Nullable<T>.HasValue 属性为 true,则是当前 System.Nullable<T> 对象的值的文本表示形式;如果System.Nullable<T>.HasValue 属性为 false,则是一个空字符串 ("")。

实例演示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Generic
{
    class Program
    {
        static void Main(string[] args)
        {  
            Nullable<int> value = 1;      
            if(value.HasValue)
            {
                string str = value.Value.ToString();
                Console.WriteLine("值为:{0}", str);
            }
            Console.ReadKey();
        }
    }
}

打印结果:

 

转载于:https://www.cnblogs.com/QingYiShouJiuRen/p/11180042.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值