可空类型的使用
可空类型也是值类型,但是它是包含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; } 实例演示: 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); 实例演示: 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(); 实例演示: 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); 实例演示: 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(); 实例演示: 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(); } } } 打印结果: (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |