草庐IT

nullable

全部标签

c# - 为什么 Nullable<T> 不是有效的自定义属性参数,而 T 是?

如果我有这样的枚举publicenumHungry{Somewhat,Very,CouldEatMySocks}和这样的自定义属性publicclassHungerAttribute:Attribute{publicHungryHungerLevel{get;set;}publicHungry?NullableHungerLevel{get;set;}}我能做到[Hunger(HungerLevel=Hungry.CouldEatMySocks)]publicclassThing1可是我做不到[Hunger(NullableHungerLevel=Hungry.CouldEatMySo

c# - 可为空的 GUID

在我的数据库中,在其中一个表中有一个允许空值的GUID列。我有一个带有Guid的方法?在表中插入新数据行的参数。但是,当我说myNewRow.myGuidColumn=myGuid时,出现以下错误:“无法隐式转换类型‘System.Guid?’到'System.Guid'。” 最佳答案 ADO.NETAPI在处理可空值类型时存在一些问题(即它无法正常工作)。我们遇到了无穷无尽的问题,因此得出的结论是最好手动将值设置为null,例如myNewRow.myGuidColumn=myGuid==null?(object)DBNull.Va

c# - 可为空的 GUID

在我的数据库中,在其中一个表中有一个允许空值的GUID列。我有一个带有Guid的方法?在表中插入新数据行的参数。但是,当我说myNewRow.myGuidColumn=myGuid时,出现以下错误:“无法隐式转换类型‘System.Guid?’到'System.Guid'。” 最佳答案 ADO.NETAPI在处理可空值类型时存在一些问题(即它无法正常工作)。我们遇到了无穷无尽的问题,因此得出的结论是最好手动将值设置为null,例如myNewRow.myGuidColumn=myGuid==null?(object)DBNull.Va

c# - 可以使用运算符吗??并抛出新的异常()?

接下来我有很多方法要做:varresult=command.ExecuteScalar()asInt32?;if(result.HasValue){returnresult.Value;}else{thrownewException();//justanexample,inmycodeIthrowmyownexception}我希望我可以像这样使用运算符??:returncommand.ExecuteScalar()asInt32???thrownewException();但它会产生编译错误。是否可以重写我的代码,或者只有一种方法可以做到这一点? 最佳答案

c# - 可以使用运算符吗??并抛出新的异常()?

接下来我有很多方法要做:varresult=command.ExecuteScalar()asInt32?;if(result.HasValue){returnresult.Value;}else{thrownewException();//justanexample,inmycodeIthrowmyownexception}我希望我可以像这样使用运算符??:returncommand.ExecuteScalar()asInt32???thrownewException();但它会产生编译错误。是否可以重写我的代码,或者只有一种方法可以做到这一点? 最佳答案

C# 反射 : How to get the type of a Nullable<int>?

我想做的是这样的:switch(myObject.GetType().GetProperty("id")){case??://whenNullable,dothiscase??://whenstring,dothiscase??://whenNullable,dothisobject.GetType()下的什么路径会有我可以使用case语句比较的数据类型的字符串名称?我需要知道类型,以便我可以拥有多个Convert.ToInt32(string)之一,它将使用反射设置myObject的值。 最佳答案 我一直在使用以下类型的代码来检查

C# 反射 : How to get the type of a Nullable<int>?

我想做的是这样的:switch(myObject.GetType().GetProperty("id")){case??://whenNullable,dothiscase??://whenstring,dothiscase??://whenNullable,dothisobject.GetType()下的什么路径会有我可以使用case语句比较的数据类型的字符串名称?我需要知道类型,以便我可以拥有多个Convert.ToInt32(string)之一,它将使用反射设置myObject的值。 最佳答案 我一直在使用以下类型的代码来检查

c# - 这种带有隐式转换运算符的 Nullable<T> 行为的理由是什么

我在Nullable之间的交互中遇到了一些有趣的行为和隐式转换。我发现从值类型为引用类型提供隐式转换允许Nullable当我期望编译错误时,将类型传递给需要引用类型的函数。下面的代码演示了这一点:staticvoidMain(string[]args){PrintCatAge(newCat(13));PrintCatAge(12);int?cat=null;PrintCatAge(cat);}privatestaticvoidPrintCatAge(Catcat){if(cat==null)System.Console.WriteLine("Whatcat?");elseSystem.

c# - 这种带有隐式转换运算符的 Nullable<T> 行为的理由是什么

我在Nullable之间的交互中遇到了一些有趣的行为和隐式转换。我发现从值类型为引用类型提供隐式转换允许Nullable当我期望编译错误时,将类型传递给需要引用类型的函数。下面的代码演示了这一点:staticvoidMain(string[]args){PrintCatAge(newCat(13));PrintCatAge(12);int?cat=null;PrintCatAge(cat);}privatestaticvoidPrintCatAge(Catcat){if(cat==null)System.Console.WriteLine("Whatcat?");elseSystem.

C#4 : Dynamic and Nullable<>

所以我有一些代码在方法之间传递这个匿名对象:varpromo=new{Text=promo.Value,StartDate=(startDate==null)?newNullable():newNullable(DateTime.Parse(startDate.Value)),EndDate=(endDate==null)?newNullable():newNullable(DateTime.Parse(endDate.Value))};接收此匿名对象类型的方法将其类型声明为dynamic:privatestaticboolIsPromoActive(dynamicpromo){ret