加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

可空类型的使用

发布时间:2020-12-15 22:39:52 所属栏目:百科 来源:网络整理
导读:可空类型也是值类型,但是它是包含null值的值类型,可以像 int? nullable = null 来表示可空类型,在C#中实际上是没有int?这种类型的,对于编译器而言,int?会被编译成Nullableint类型,即可空类型。 让我们来看看Nullableint中常见的方法属性: 1 . public

可空类型也是值类型,但是它是包含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();
        }
    }
}

打印结果:

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读