草庐IT

list_type

全部标签

c# - 为什么会有 List<T>.BinarySearch(...)?

我正在查看List,我看到一个带有一些重载的BinarySearch方法,我不禁想知道在List中使用这样的方法是否有意义?除非列表已排序,否则为什么我要进行二分查找?如果列表未排序,调用该方法只会浪费CPU时间。在List上使用该方法有什么意义? 最佳答案 除了其他正确答案外,我还注意到二分查找出奇地难以正确编写。有很多极端情况和一些棘手的整数运算。由于二分搜索显然是排序列表上的常见操作,BCL团队通过一次正确编写二分搜索算法而不是鼓励客户都编写自己的二分搜索算法来为世界提供服务;很多客户编写的算法都是错误的。

c# - List<> Capacity 返回的项目多于添加的项目

List上有几个属性这似乎与列表中的项目数量有关-Capacity,Count(作为属性和方法存在)。这非常令人困惑,尤其是与Array相比那只有Length.我正在使用List.Capacity但它给出了意想不到的结果:Listfruits=newList();fruits.Add("apple");fruits.Add("orange");fruits.Add("banana");fruits.Add("cherry");fruits.Add("mango");Console.WriteLine("theListhas{0}itemsinit.",fruits.Capacity);

c# - 带有通用对象列表的 List.Contains(item)

如果您有一个列表,如果指定的属性或属性集合存在,您如何返回该项目?publicclassTesting{publicstringvalue1{get;set;}publicstringvalue2{get;set;}publicintvalue3{get;set;}}publicclassTestingList{publicvoidTestingNewList(){vartestList=newList{newTesting{value1="Value1-1",value2="Value2-1",value3=3},newTesting{value1="Value1-2",value2

c# - VS2010 'Show Derived Types' 类 View 中缺少选项

我正在VisualStudio2010中处理VisualC#项目,并在类View中浏览各种类型。根据this文档中,类View设置菜单中应该有一个类似于“显示基本类型”选项的“显示派生类型”选项。然而,它似乎不见了。我还没有设法找到任何错误报告或对这个问题的引用,所以我想知道我是否只是遗漏了一些东西。 最佳答案 我在VisualStudio2010中看到它,这是一个具有派生类型的C++项目。但是,在使用派生类型提到的C#项目中,未提供此选项。我正在检查一个没有派生类型的项目,看看这是否会导致该选项被禁用。编辑:似乎没有派生类型的项目

c# - 为什么不能将 Type 用作常量值?

引用MSDN-const(C#reference):Aconstantexpressionisanexpressionthatcanbefullyevaluatedatcompiletime.Therefore,theonlypossiblevaluesforconstantsofreferencetypesarestringandanullreference.根据:typeof(T)vs.Object.GetType()performance,typeof(T)是一个编译时表达式。那么为什么Type不能是常量值呢?以下代码不会编译:publicconstTypeINT_TYPE=ty

c# - .NETStandard 1.0/.NET Core 中 Type.GetGenericArguments() 的等价物是什么?

.NETStandard1.0中“缺少”方法System.Type.GetGenericArguments(),我认为TypeInfo.GenericTypeArguments是的替代品GetGenericArguments(),但不幸的是,当提供开放泛型类型时,它们的行为会有所不同。以下面的代码为例:Typetype=typeof(ICommandHandler);type.GetGenericArguments();//return{TCommand}type.GetTypeInfo().GenericTypeArguments;//returnsemptyarray虽然GetGe

c# - Dapper 抛出 "Invalid type owner for DynamicMethod."

所以我正在尝试使用Dapper.net,而且我很喜欢它。我不喜欢的是当我尝试批量插入实体时抛出以下错误:DynamicMethod的类型所有者无效。atSystem.Reflection.Emit.DynamicMethod.Init(Stringname,MethodAttributesattributes,CallingConventionscallingConvention,TypereturnType,Type[]signature,Typeowner,Modulem,BooleanskipVisibility,BooleantransparentMethod,StackCra

c# - 如何从基类中获取子类的 `Type`

我有一个抽象基类,我想在其中实现一个方法来检索继承类的特性属性。像这样的……publicabstractclassMongoEntityBase:IMongoEntity{publicvirtualobjectGetAttributeValue(stringpropertyName)whereT:Attribute{varattribute=(T)typeof(this).GetCustomAttribute(typeof(T));returnattribute!=null?attribute.GetType().GetProperty(propertyName).GetValue(a

c# - XML 序列化错误 : 2 types both use the XML type name, 'Relationship',来自命名空间 ''

我在通过XML进行序列化时遇到问题,因为2个类使用一个类(尽管类不同!)称为关系。我尝试使用XML属性用另一个名称装饰其中的一个类,但它仍然给我以下错误:{"Types'SiteServer.Relationship'and'LocalServer.Relationship'bothusetheXMLtypename,'Relationship',fromnamespace''.UseXMLattributestospecifyauniqueXMLnameand/ornamespaceforthetype."}这是我的2个类(class),有人知道为什么吗??我使用了错误的属性吗?它似

c# - 保留一个 Dictionary<Type, MyClass<T>> ,其中元素可以按类型引用

我有一个名为EntityTypeTransform的抽象类,它有一个抽象方法,用于保存将IDataRecord转换为T实例的Func委托(delegate)。publicabstractclassEntityTypeTransformwhereTEntityType:class{publicabstractFuncGetDataTransform();}该类的实现可能看起来像(看起来像):publicclassTaskParameterEntityTypeTransform:EntityTypeTransform{publicoverrideFuncGetDataTransform()