草庐IT

PF_NULLABLE

全部标签

c# - 通过反射设置属性 Nullable<>

我尝试动态设置一个Nullable属性。我得到我的属性(property)ex:PropertyInfoproperty=class.GetProperty("PropertyName");//MypropertyisNullableatthistimeSothetypecouldbeastringorint我想像这样通过反射来设置我的属性property.SetValue(class,"1256",null);当我的属性是NullableGeneric时它不起作用。所以我试图找到一种方法来设置我的属性。了解我执行的可空属性的类型Nullable.GetUnderlyingType(p

c# - 为什么 == 运算符在未定义 == 时对 Nullable<T> 起作用?

我只是在看thisanswer,其中包含Nullable的代码从.NETReflector,我注意到两件事:从Nullable开始时需要显式转换至T.==运算符未定义。鉴于这两个事实,编译结果令我感到惊讶:int?value=10;Assert.IsTrue(value==10);使用代码value==10,value正在神奇地转换为int(因此允许使用int的==运算符,或者==运算符被神奇地定义为Nullable。(或者,我认为不太可能,Reflector遗漏了一些代码。)我希望必须执行以下操作之一:Assert.IsTrue((value.Equals(10));//worksb

c# - 哪个是首选 : new Nullable<int> or (int? )null?

在这样的表达式中首选哪种方式:int?Id{get{inti;returnInt32.TryParse(Request["id"],outi)?i:(int?)null;}}在null上转换更好吗?或者创建一个newNullable? 最佳答案 最好的是default(int?),因为它总是按预期工作(引用类型、值类型,甚至在泛型中)。 关于c#-哪个是首选:newNullableor(int?)null?,我们在StackOverflow上找到一个类似的问题:

c# - Nullable<bool> 类型有什么用?

bool变量可以是true或false,而bool?也可以是null。为什么我们需要bool的第三个值?如果它不是true,无论它是什么,它都是==false你能建议一个我喜欢bool?的场景吗?谢谢 最佳答案 出于多种原因,某些内容可能为真、假或未定义。如何回答“你的第三个child是女孩?”如果一个人只有两个child呢?true和false都不正确。Null表示比较不适用。 关于c#-Nullable类型有什么用?,我们在StackOverflow上找到一个类似的问题:

c# - 默认模型绑定(bind)器不绑定(bind) IEnumerable 中的 Nullable 类型

我有一个Controller操作,其定义如下所示-publicActionResultChangeModel(IEnumerableinfo,long?destinationId)和模型:publicclassMyModel{publicstringName;//Getspopulatedbydefaultbinderpubliclong?SourceId;//remainsnullthoughthevalueissetwheninvoked}'Name'属性在Controller操作中被填充,但是SourceId属性保持为空。destinationId是一个long?参数也会被填充。

c# - 为什么不能使用 is 运算符来区分 bool 和 Nullable<bool>?

我遇到了这个,很好奇为什么不能使用is运算符区分bool和Nullable?示例;voidMain(){booltheBool=false;NullabletheNullableBoolThatsFalse=false;NullabletheNullableBoolThatsNull=null;voidWhatIsIt(objectvalue){if(valueisbool)Console.WriteLine("It'sabool!");if(valueisNullable)Console.WriteLine("It'saNullable!");if(valueisnull)Conso

c# - 我可以将 DataContractSerializer 配置为不在输出 XML 中创建可选(即 Nullable<> 和 List<>)元素吗?

我正在使用新的.NET3.0DataContractSerializer。我有要序列化的Nullable>和List>对象。示例:[DataContract(Namespace="")]classTest{publicstaticvoidGo(){Testtest=newTest();vardcs=newDataContractSerializer(typeof(Test));dcs.WriteObject(newStreamWriter("test.xml").BaseStream,test);}[DataMember]publicNullableNullableNumber=nul

c# - Nullable<T> 类型在幕后如何工作?

我很想知道Nullable类型在幕后是如何工作的。它是否正在创建一个可能值为null的新对象(可以为对象分配null)?在我们使用Nullable的示例中,当您为它分配一个空值时,它们是从对象到int的某种隐式转换,反之亦然吗?我也知道如何手动创建它,使用Nullable类型比我们自己创建它有好处吗? 最佳答案 可空类型是一个由两个字段组成的结构:bool和T。当值为null时,bool为false,T具有默认值。当值不为null时,bool为真。与自己实现功能相比,使用Nullable有两个主要好处。有语言支持,正如ChaosPa

c# - 如果 typeof(int?) 是 Int32,Nullable.GetUnderlyingType 有什么用?

为什么typeofint?是Int32int?x=1;Console.WriteLine(x.GetType().Name);如果可以,那么Nullable.GetUnderlyingType有什么用? 最佳答案 调用GetType()装箱你的变量。CLR有一条特殊规则Nullable装箱到T.所以x.GetType将返回Int32而不是Nullable.int?x=1;x.GetType()//Int32typeof(int?)//Nullable自Nullable包含null将装箱到null以下将引发异常:int?x=null;

c# - 将 nullable double 设置为 null 有什么问题?

这个问题在这里已经有了答案:关闭12年前。PossibleDuplicates:Whycan'tIsetanullableinttonullinaternaryifstatement?Nullabletypesandtheternaryoperator.Whywon'tthiswork?下面有什么问题publicdouble?Progress{get;set;}Progress=null;//worksProgress=1;//worksProgress=(1==2)?0.0:null;//failsTypeofconditionalexpressioncannotbedetermin