草庐IT

c# - 为什么 Enum 的 HasFlag 方法需要装箱?

我正在阅读“C#viaCLR”,在第380页上,有一条说明如下:NoteTheEnumclassdefinesaHasFlagmethoddefinedasfollowspublicBooleanHasFlag(Enumflag);Usingthismethod,youcouldrewritethecalltoConsole.WriteLinelikethis:Console.WriteLine("Is{0}hidden?{1}",file,attributes.HasFlag(FileAttributes.Hidden));However,Irecommendthatyouavoid

c# - 为什么这个从 short 到 int 的转换会失败?

我们有一些代码可以将MicrosoftAccess数据库中的数据归档到MSSQLServer数据库中。假设我们已经从Access表中填充了一个数据读取器,并且我们正在向SqlCommand添加一个参数以准备插入,那么我们有一个失败的类型转换。这是代码:oSqlServerDbCmd_ForInsert.Parameters.AddWithValue("@Duration",(int)oReader["Duration"]);oReader中的字段实际上是一个AccessInteger,在C#中是一个short。如果我们在这里转换为short就没有问题。但是,如果我们转换为int,代码将

c# - Array 中会发生装箱和拆箱吗?

我是编程新手,根据MSDN,Boxingistheprocessofconvertingavaluetypetothetypeobjectortoanyinterfacetypeimplementedbythisvaluetype.WhentheCLRboxesavaluetype,itwrapsthevalueinsideaSystem.Objectandstoresitonthemanagedheap.Unboxingextractsthevaluetypefromtheobject.Boxingisimplicit;unboxingisexplicit.我知道我们可以在数组列表中

c# - 通用约束如何防止使用隐式实现的接口(interface)对值类型进行装箱?

我的问题与这个问题有些相关:Explicitlyimplementedinterfaceandgenericconstraint.但是,我的问题是编译器如何启用泛型约束以消除对显式实现接口(interface)的值类型进行装箱的需要。我想我的问题可以归结为两个部分:在访问显式实现的接口(interface)成员时要求对值类型进行装箱的幕后CLR实现发生了什么,以及删除此要求的通用约束会发生什么情况?一些示例代码:internalstructTestStruct:IEquatable{boolIEquatable.Equals(TestStructother){returntrue;}}

c# - 转换到界面是拳击转换吗?

我有一个接口(interface)IEntitypublicinterfaceIEntity{boolValidate();}我有一个实现这个接口(interface)的Employee类publicclassEmployee:IEntity{publicboolValidate(){returntrue;}}现在如果我有以下代码Employeeemp1=newEmployee();IEntityent1=(IEntity)emp1;//Isthisaboxingconversion?如果不是拳击转换,那么类型转换是如何运作的? 最佳答案

c# - 枚举装箱和平等

为什么会返回FalsepublicenumDirections{Up,Down,Left,Right}staticvoidMain(string[]args){boolmatches=IsOneOf(Directions.Right,Directions.Left,Directions.Right);Console.WriteLine(matches);Console.Read();}publicstaticboolIsOneOf(Enumself,paramsEnum[]values){foreach(varvalueinvalues)if(self==value)returntru

c# - 为什么委托(delegate)逆变对值类型不起作用?

此代码段未在LINQPad中编译。voidMain(){(new[]{0,1,2,3}).Where(IsNull).Dump();}staticboolIsNull(objectarg){returnarg==null;}编译器的错误信息是:Nooverloadfor'UserQuery.IsNull(object)'matchesdelegate'System.Func'它适用于字符串数组,但不适用于int[]。这显然与拳击有关,但我想知道详细信息。 最佳答案 给出的答案(值类型不存在差异)是正确的。当可变类型参数之一是值类型时

c# - 装箱和拆箱 : when does it come up?

所以我明白了什么是装箱和拆箱。它什么时候出现在现实世界的代码中,或者在什么例子中它是一个问题?我无法想象做这样的事情:inti=123;objecto=i;//Boxingintj=(int)o;//Unboxing...但这几乎可以肯定是过于简单化了,我什至可能在不知不觉中进行了装箱/拆箱。 最佳答案 与泛型之前相比,现在问题不大了。现在,例如,我们可以使用:Listx=newList();x.Add(10);inty=x[0];根本不需要装箱或拆箱。以前,我们有:ArrayListx=newArrayList();x.Add(1

c# - 为什么在值类型上调用显式接口(interface)实现会导致它被装箱?

我的问题与这个问题有些相关:Howdoesagenericconstraintpreventboxingofavaluetypewithanimplicitlyimplementedinterface?,但有所不同,因为它根本不是通用的,因此不需要约束来执行此操作。我有密码interfaceI{voidF();}structC:I{voidI.F(){}}staticclassP{staticvoidMain(){Cx;((I)x).F();}}主要方法编译成这样:IL_0000:ldloc.0IL_0001:boxCIL_0006:callvirtinstancevoidI::F()

c# - 将结构转换为通用接口(interface)时是否有装箱/拆箱?

这个问题在这里已经有了答案:关闭9年前。PossibleDuplicate:Structs,InterfacesandBoxing来自MSDN:http://msdn.microsoft.com/en-us/library/yz2be5wk.aspxBoxingistheprocessofconvertingavaluetypetothetypeobjectortoanyinterfacetypeimplementedbythisvaluetype.但是通用接口(interface)呢?例如,int派生自IComparable和IComparable.假设我有以下代码:voidfoo(