#pragma warning disable CS0219 // Variable is assigned but its value is never used static void Main() { Nullable<int> noValue = new Nullable<int>(); // Console.WriteLine(noValue.GetType()); Nullable<int> someValue = new Nullable<int>(5); Console.WriteLine(someValue.GetType()); }
/// <summary> /// 装箱拆箱 /// </summary> public void BoxedAndUnboxen() { Nullable <int> y = 5; int? x = null; Console.WriteLine("装箱前,y类型{0}", y.GetType()); //没有值得可空类型调用函数时,会报错。 //因为在调用函数之前,编译器会对可空类型进行装箱操作,变成空引用。 //然后就抛出了空引用异常 //Console.WriteLine("x类型{0}", x.GetType()); //Console.WriteLine("x为空,null直接赋值"); object obj = y; Console.WriteLine("装箱后,y类型{0}", y.GetType()); Console.WriteLine("obj类型{0}", obj.GetType()); Console.WriteLine("obj值为:{0}", obj); int value = (int)obj; x = (int?)obj; Console.WriteLine("拆箱后,obj类型{0}", obj.GetType()); Console.WriteLine("拆箱后,value类型{0}", value.GetType()); Console.WriteLine("拆箱后,x类型{0}", x.GetType()); Nullable <int> isnull = null; //没有值得可空类型调用函数时,会报错。 //isnull.GetType(); object objj = isnull; Console.WriteLine("值为:" + objj); int unboxed = (int)objj; Console.WriteLine(); }
public static object GetDataForDBfromNullable <T>(Nullable <T> value) where T : struct { if (value.HasValue) { if (value.GetType() == Type.GetType("System.Boolean")) { return(value.Value); } } if (value.HasValue && !value.Value.Equals(default(T))) { return(value.Value); } return(DBNull.Value); }
static void Main(string[] args) { int? num = null; bool isHasValue = num.HasValue; Console.WriteLine(isHasValue); Console.WriteLine(num.GetHashCode()); Nullable <int> x = 5; object boxed = x; Console.WriteLine(x.GetType()); int normal = (int)boxed; Console.WriteLine(normal.GetType()); int?nullable = 5; nullable = (int?)boxed; Console.WriteLine(nullable); nullable = new Nullable <int>(); boxed = nullable; Console.WriteLine(boxed == null); nullable = (int?)boxed; Console.WriteLine(nullable.HasValue); Console.WriteLine("/*************/"); Console.WriteLine("Instance with value:"); Display(x); Console.WriteLine("Instance without value:"); x = null; Display(x); Console.WriteLine("-------------------------------------\n\n"); Console.WriteLine(Nullable.Compare <int>(null, 1)); int? xx = null; Type nullType = xx.GetType(); Type underlyingType = Nullable.GetUnderlyingType(nullType); Console.WriteLine(Nullable.GetUnderlyingType(xx.GetType()).FullName); Console.ReadLine(); }
static void Main(string[] args) { Nullable <int> noValue = new Nullable <int>(); object noValueBoxed = noValue; //Boxes a value where HasValue is false Console.WriteLine(noValueBoxed == null); //Prints True: the result of boxing is a null reference. Nullable <int> someValue = new Nullable <int>(5); object someValueBoxed = someValue; //Boxes a value where HasValue is true Console.WriteLine(someValueBoxed.GetType()); //Prints System.Int32: the result is a boxed int Nullable <int> noValueNew = new Nullable <int>(); //Console.WriteLine(noValueNew.GetType()); //Would throw NullReferenceException Nullable <int> someValueNew = new Nullable <int>(5); Console.WriteLine(someValueNew.GetType()); //Prints System.Int32, the same as if you’d used typeof(int) }
public static void Test() { Nullable <int> j = null; j = 1; Console.WriteLine("Nullable<int> type is:" + j.GetType()); // System.Int32 var customer = new NullClass(); Console.WriteLine(customer); Console.WriteLine(Nullable.Compare(customer.i, new NullClass().i)); customer.Init(); Console.WriteLine(customer); string a = null; string b = "2"; string c = "3"; Console.WriteLine(a ?? null ?? b ?? null ?? c ?? null); Console.ReadKey(); }
private IQueryable <T> Simplified <T>(IQueryable <T> query, PropertyInfo propertyInfo, Nullable <bool> propertyValue) { ConstantExpression c; ParameterExpression e = Expression.Parameter(typeof(T), "e"); MemberExpression m = Expression.MakeMemberAccess(e, propertyInfo); if (!propertyValue.HasValue) { c = Expression.Constant(null); } else { c = Expression.Constant(propertyValue.Value, propertyValue.GetType()); } BinaryExpression b = Expression.Equal(m, c); Expression <Func <T, bool> > lambda = Expression.Lambda <Func <T, bool> >(b, e); return(query.Where(lambda)); }
// 可空类型装箱和拆箱的演示 private static void BoxedandUnboxed() { // 定义一个可空类型对象nullable Nullable <int> nullable = 5; int? nullablewithoutvalue = null; //Console.Write(nullablewithoutvalue.GetType());//这个会报错 // 获得可空对象的类型,此时返回的是System.Int32,而不是System.Nullable<System.Int32>,这点大家要特别注意下的 Console.WriteLine("获取不为null的可空类型的类型为:{0}", nullable.GetType()); // 对于一个为null的类型调用方法时出现异常,所以一般对于引用类型的调用方法前,最好养成习惯先检测下它是否为null //Console.WriteLine("获取为null的可空类型的类型为:{0}", nullablewithoutvalue.GetType()); // 将可空类型对象赋给引用类型obj,此时会发生装箱操作,大家可以通过IL中的boxed 来证明 object obj = nullable; // 获得装箱后引用类型的类型,此时输出的仍然是System.Int32,而不是System.Nullable<System.Int32> Console.WriteLine("获得装箱后obj 的类型:{0}", obj.GetType()); // 拆箱成非可空变量 int value = (int)obj; Console.WriteLine("拆箱成非可空变量的情况为:{0}", value); // 拆箱成可空变量 nullable = (int?)obj; Console.WriteLine("拆箱成可空变量的情况为:{0}", nullable); // 装箱一个没有值的可空类型的对象 obj = nullablewithoutvalue; Console.WriteLine("对null的可空类型装箱后obj 是否为null:{0}", obj == null); // 拆箱成非可空变量,此时会抛出NullReferenceException异常,因为没有值的可空类型装箱后obj等于null,引用一个空地址 // 相当于拆箱后把null值赋给一个int 类型的变量,此时当然就会出现错误了 //value = (int)obj; //Console.WriteLine("一个没有值的可空类型装箱后,拆箱成非可空变量的情况为:{0}", value); // 拆箱成可空变量 nullable = (int?)obj; Console.WriteLine("一个没有值的可空类型装箱后,拆箱成可空变量是否为null:{0}", nullable == null); }
private static void BoxedandUnboxed() { Nullable <int> nullable = 5; int? nullablewithoutvalue = null; Console.WriteLine($"获取不为null的可空类型的类型:{nullable.GetType()}"); object obj = nullable; Console.WriteLine($"装箱后obj的类型{obj.GetType()}"); int value = (int)obj; Console.WriteLine($"拆箱成非可空变量的情况{value}"); nullable = (int?)obj; Console.WriteLine($"拆箱成非可空变量的情况{nullable}"); obj = nullablewithoutvalue; Console.WriteLine($"对null的可空类型装箱后obj是否为null:{obj == null}"); nullable = (int?)obj; Console.WriteLine($"一个没有值的可空类型装箱后,拆箱成可空变量是否为null:{0}"); }
static void TestBoxed() { Nullable<int> iValue = new Nullable<int>(5); // 等价于Nullable<int> iValues = 5; 存在着从T到Nullable<T>隐式的类型转换 // 从Nullable<T>到T的转换需要显示指明。 Console.WriteLine("获取不为null的可用类型的类型为: {0}", iValue.GetType()); int i = (int)iValue; Object obj = iValue;//装箱过程 Console.WriteLine("获得装箱后obj的类型为: {0}", obj.GetType()); i = (int)obj; int? iNull = (Nullable<int>)obj; int? iInit = new int?(); // Nullable<int> iInit = new Nullable<int>(); // 对于一个为null的类型调用方法时会出现异常,所以一般对引用类型的调用方法前 // 要进行非null的判断,但是对于Nullable<T>类型为null时,可以调用HasValue特性 // 这是一个特列。 //Console.WriteLine("获取不为null的可用类型的类型为: {0}", iValue.GetType()); Console.WriteLine("iInit is null: {0}", iInit.HasValue); obj = iInit; iNull = (Nullable<int>)obj; //i = (int)obj; Console.WriteLine(iNull.HasValue); }
public static void BindControlsToObject(object obj, Control container) { if (obj == null) { return; } //获取业务对象的属性 Type objType = obj.GetType(); PropertyInfo[] objPropertiesArray = objType.GetProperties(); foreach (PropertyInfo objProperty in objPropertiesArray) { Control control = FindControl(container, objProperty.Name); if (control == null) { continue; } if (control is DateTimePicker) { DateTimePicker dateTimePicker = (DateTimePicker)control; System.Nullable <DateTime> dt = (DateTime?)dateTimePicker.Value; objProperty.SetValue(obj, Convert.ChangeType(dt, dt.GetType()), null); } else if (control is NumericUpDown) { NumericUpDown numericUpDown = (NumericUpDown)control; Nullable <decimal> i = (decimal?)numericUpDown.Value; objProperty.SetValue(obj, Convert.ChangeType(i, i.GetType()), null); } else if (control is ComboBox) { ComboBox comboBox = (ComboBox)control; objProperty.SetValue(obj, Convert.ChangeType(comboBox.Text, objProperty.PropertyType), null); } else { Type controlType = control.GetType(); PropertyInfo[] controlPropertiesArray = controlType.GetProperties(); bool success = false; success = FindAndGetControlProperty(obj, objProperty, control, controlPropertiesArray, "Checked", typeof(bool)); if (!success) { success = FindAndGetControlProperty(obj, objProperty, control, controlPropertiesArray, "Value", typeof(String)); } if (!success) { success = FindAndGetControlProperty(obj, objProperty, control, controlPropertiesArray, "Text", typeof(String)); } if (!success) { success = FindAndGetControlProperty(obj, objProperty, control, controlPropertiesArray, "SelectedValue", typeof(String)); } } } }
static void Main(string[] args) { Nullable <int> c = 20; Console.WriteLine(c.GetType()); //shorthand of Nullable type int?a = 10; int?b = null; Console.WriteLine(a.HasValue); //True bcoz a=10 Console.WriteLine(b.HasValue); // False bcoz b=null Console.WriteLine(a.GetValueOrDefault()); //If not null, return value--10 Console.WriteLine(b.GetValueOrDefault()); //If null, return 0 value int d = 10; int result = (int)a * d; //arises some we cannot convert int? to int ,explicit typecasting Console.WriteLine(result); int[] arr = new int[3] { 3, 4, 5 }; int?[] arr1 = new int?[2]; arr1[0] = 3; arr1[1] = null; int?[] cc = new int?[2] { null, 39 }; Console.WriteLine("Nullable array elements"); foreach (int?i in cc) { Console.WriteLine(i.GetValueOrDefault()); } //Compare Nullable type with Non-Nullable type is possible... if (a == d) { Console.WriteLine("Equal"); } int?result1 = a * d; //Implicitly convert to int? ,Nullable type... Console.WriteLine(result1.HasValue); //null-coalescing operator(??) int?r = b ?? 110; //If a is null then 0 is assigned to r otherwise value is assigned to r Console.WriteLine(r); Console.WriteLine(r.Value); int?x = null; int y = 3; int z = x.GetValueOrDefault() * y; Console.WriteLine(z); Console.WriteLine(string.Format("{0,10} {1,-10}", "veerababu", "rallabandi")); int?bb = say(2, 3); Console.WriteLine(say(2, 3));//callign method, it is return nullable }
public void Test3() { var n = new Nullable <int>(); Assert.Throws <NullReferenceException>(() => n.GetType()); }