jjzjj

Nullable

全部标签

c# - 为什么可空值序列的 Linq-to-Objects 总和本身可以为空?

像往常一样,int?表示System.Nullable(或System.Nullable`1[System.Int32])。假设您在内存中有一个IEnumerable(例如List),我们称它为seq;然后你可以找到它的总和:varseqSum=seq.Sum();当然这会转到扩展方法重载int?IEnumerable.Sum()(documentation)这实际上是System.Linq.Enumerable上的静态方法.但是,该方法永远不会返回null,那么为什么返回类型声明为Nullable?即使在seq的情况下是一个空集合,或者更一般地说,是一个所有元素都是null的集合类型

c# - Code Contracts 是否未能发现 Nullable<T>.HasValue 和 null 之间的明显关系?

我正在尝试将代码契约应用到我的代码中,但我遇到了一个令人费解的问题。这段代码不符合契约(Contract),但除非我真的很厚,否则我希望它能够轻松分析id在返回时必须有一个值if(id==null)thrownewInvalidOperationException(string.Format("{0}'{1}'doesnotyethaveanidentity",typeof(T).Name,entity));returnid.Value; 最佳答案 我已经弄清了这个行为的真相,这不是CodeContract的错。我在ILSpy中打开

c# - C# 中的 `x is int?` 和 `x is int` 有区别吗?

classCwhereT:struct{boolM1(objecto)=>oisT;boolM2(objecto)=>oisT?;}上面的两种方法在传递null时似乎表现相同引用或盒装T值(value)。但是,生成的MSIL代码有点不同:.methodprivatehidebysiginstanceboolM1(objecto)cilmanaged{.maxstack8IL_0000:ldarg.1IL_0001:isinst!TIL_0006:ldnullIL_0007:cgt.unIL_0009:ret}对比.methodprivatehidebysiginstanceboolM2

c# - DisplayFormat 不适用于 Nullable<DateTime>

我试图在MVC中格式化一些DateTimes,但DisplayFormat没有应用于Nullable对象,我不知道为什么。它在CreatedDateTime上运行良好,但在LastModifiedDateTime上运行良好[DisplayFormat(ApplyFormatInEditMode=true,DataFormatString="{0:MM/dd/yyhh:mmtt}")]publicDateTimeCreatedDateTime{get;set;}[DisplayFormat(ApplyFormatInEditMode=true,DataFormatString="{0:M

c# - C# 是 int 吗?当 hasvalue = true 时,bool 总是被装箱吗?

ThisMSDNreference似乎表明当int?(或任何Nullable)有一个值,它总是装箱(因此数据存储效率低得多,内存方面比int)。是这样吗? 最佳答案 该页面指的是当您装箱Nullable时结构,而不是结构本身内部的值。在您尝试对可空类型本身进行装箱之前,存储可空类型不涉及装箱:int?a=42;//noboxingint?n=null;//noboxingobjectnObj=n;//noboxingobjectaObj=a;//onlynowwillboxingoccur此行为与装箱常规值类型(处理null情况除外

c# - 在 Linq-To-Sql 中避免 "Nullable object must have a value."

我有一个这样的方法查询:publicIListGetBusinessObject(Guid?filterId){using(vardb=newL2SDataContext()){varresult=fromboindb.BusinessObjectswhere(filterId.HasValue)?bo.Filter==filterId.value:trueorderbybo.NameselectSqlModelConverters.ConvertBusinessObject(bo);returnresult.ToList();}}在运行时,这会抛出一个System.InvalidOp

c# - 在单个 C# 泛型方法中返回 nullable 和 null?

是否可以在C#泛型方法中返回对象类型或Nullable类型?例如,如果我有一个List的安全索引访问器我想返回一个值,以后可以用==null检查或.HasValue().我目前有以下两种方法:staticT?SafeGet(Listlist,intindex)whereT:struct{if(list==null||index=list.Count){returnnull;}returnlist[index];}staticTSafeGetObj(Listlist,intindex)whereT:class{if(list==null||index=list.Count){return

c# - 类型 'MyObject' 必须是不可为 null 的值类型才能将其用作泛型类型或方法 'T' 中的参数 'Nullable<T>'

我正在使用.netframework4.5我得到以下错误ErrorCS0453Thetype'MyObject'mustbeanon-nullablevaluetypeinordertouseitasparameter'T'inthegenerictypeormethod'Nullable'publicasyncTask>MyMethod(stringmyParamter){}我也试过publicasyncTaskMyMethod(stringmyParamter){}如果我将事物设置为可为空,那么为什么我会在方法名称下方看到一条红线并显示此错误消息计算器answer很简单,使返回类型

c# - 为什么我不能写 Nullable<Nullable<int>>?

Nullable的定义是:[SerializableAttribute]publicstructNullablewhereT:struct,new()约束whereT:struct暗示T只能是值类型。所以我很清楚我不能写:Nullablea;//error.makessensetome因为string是引用类型,不是值类型。但是我真的不明白为什么我不能写Nullable>b;//error.butwhy?为什么不允许?毕竟,Nullable是一个值类型,因此,它可以是Nullablle的类型参数.当我在ideone上编译它时,它给出了这个错误(ideone):errorCS0453:T

c# - 在 C# 中使用条件运算符键入结果

我正在尝试使用条件运算符,但我对它认为结果应该是的类型感到困惑。下面是我设法展示我遇到的问题的示例:classProgram{publicstaticvoidOutputDateTime(DateTime?datetime){Console.WriteLine(datetime);}publicstaticboolIsDateTimeHappy(DateTimedatetime){if(DateTime.Compare(datetime,DateTime.Parse("1/1"))==0)returntrue;returnfalse;}staticvoidMain(string[]ar