草庐IT

buts_list

全部标签

c# - 为什么 List<T> 出现 "Index was out of range"异常而不是数组?

当我初始化数组并使用索引器访问元素时,效果很好:object[]temp=newobject[5];temp[0]="bar";现在我希望同样适用于List,假设您可以通过将容量传递给构造函数来初始化它:Listtemp=newList(5);temp[0]="bar";然而,最后一行抛出以下异常:Indexwasoutofrange.Mustbenon-negativeandlessthanthesizeofthecollection为什么List会发生这种情况类型,但不是数组?由于数组只是CLR集合的较低级别抽象,那么为什么会出现此异常?原创question通过AwaisMahmo

C# 设计 : Why is new/override required on abstract methods but not on virtual methods?

为什么抽象方法需要new/override而虚方法不​​需要?示例1:abstractclassShapesClass{abstractpublicintArea();//abstract!}classSquare:ShapesClass{intx,y;publicintArea()//Error:missing'override'or'new'{returnx*y;}}编译器会显示这个错误:要使当前成员覆盖该实现,请添加override关键字。否则添加新关键字示例2:classShapesClass{virtualpublicintArea(){return0;}//itisvirt

c# - 在List<T>中选择整周没有休息日的记录 - C#

我有一个定义如下的Employee类:Employee{publicintId{get;set;}publicstringName{get;set;}publicDateTimeWorkDate{get;set;}publicboolisOff{get;set;}}这是我的类实现和用法:Listworkers=newList(){newEmployee{Id=1,Name="Emp1",WorkDate=Convert.ToDateTime("4/11/2016"),IsOff=false},newEmployee{Id=1,Name="Emp1",WorkDate=Convert.T

c# - 使用查询表达式对 List<T> 进行排序

我在使用Linq订购这样的结构时遇到问题:publicclassPerson{publicintID{get;set;}publicListAttributes{get;set;}}publicclassPersonAttribute{publicintID{get;set;}publicstringName{get;set;}publicstringValue{get;set;}}一个人可能会这样:PersonAttributeAge=newPersonAttribute{ID=8,Name="Age",Value="32"};PersonAttributeFirstName=new

C# EWS 托管 API : How to access shared mailboxes but not my own inbox

如何连接到交换服务器并从共享邮箱(不是我自己的“myname@mycompany.com”)读取邮件。到目前为止,这是我的代码://CreateaserviceExchangeServiceservice=newExchangeService(ExchangeVersion.Exchange2007_SP1);//Autodiscoverendpointservice.AutodiscoverUrl("someaddress@mycompany.com");FindFoldersResultsfolderSearchResults=service.FindFolders(WellKno

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# - 使用 List<string> 替换字符串

我有一个我想忽略的单词列表:publicListignoreList=newList(){"North","South","East","West"};对于给定的字符串,比如"14thAvenueNorth"我希望能够删除“North”部分,所以基本上是一个返回"14thAvenue"调用时。我觉得我应该可以通过混合使用LINQ、正则表达式和替换来完成某些事情,但我就是想不通。更大的图景是,我正在尝试编写一个地址匹配算法。在使用Levenshtein算法评估相似度之前,我想过滤掉“街道”、“北方”、“大道”等词。 最佳答案 这个怎么

C# 用 LINQ 用相同的模式替换 List<string> 的所有元素

我有一个包含数千个字符串的C#列表:"2324343""6572332""45122"...我想用括号把它们全部替换掉​​,这样它们看起来像"(2324343)""(6572332)""(45122)"...我知道如何编写for循环并执行此操作,但我想知道是否有一种方法可以更好地使用LINQ和LAMBDA表达式来执行此操作。我也愿意接受其他建议。非常感谢! 最佳答案 varresultList=list.Select(x=>string.Format("({0})",x)).ToList();

c# - 无法加载文件或程序集 XXX 或其依赖项之一。找到的程序集的 list 定义与程序集引用不匹配

在本地运行Asp.NetMVC应用程序时,一切正常,但在服务器上部署应用程序时,出现此错误。Couldnotloadfileorassembly'WebGrease,Version=1.5.1.25624,Culture=neutral,PublicKeyToken=31bf3856ad364e35'oroneofitsdependencies.Thelocatedassembly'smanifestdefinitiondoesnotmatchtheassemblyreference.(ExceptionfromHRESULT:0x80131040)我最近刚刚通过使用WebGrease

c# - 将 List<string> 转换为 StringCollection

我需要上述功能,因为我只能将StringCollection存储到设置,而不能存储字符串列表。如何将List转换为StringCollection? 最佳答案 怎么样:StringCollectioncollection=newStringCollection();collection.AddRange(list.ToArray());或者,避免中间数组(但可能涉及更多重新分配):StringCollectioncollection=newStringCollection();foreach(stringelementinlist)