草庐IT

collection_foo_bar

全部标签

c# - 通过 ref : cannot convert from 'Foo' to 'ref IFoo' 传递实现

这个问题在这里已经有了答案:Whydoesn't'ref'and'out'supportpolymorphism?(10个答案)关闭3年前。有人可以向我解释为什么这在C#中是不正确的吗:namespaceNamespaceA{publicclassClassA{publicinterfaceIInterfaceA{StringProperty{set;}}}}namespaceNamespaceB{publicclassClassB{publicclassImpA:NamespaceA.ClassA.IInterfaceA{privateStringmProperty;publicSt

c# - .NET 4.0 System.Collections.Concurrent 集合在 .NET 3.0 SynchronizedCollection 的功能中添加了什么?

.NET4.0引入了System.Collections.Concurrent命名空间:"TheSystem.Collections.Concurrentnamespaceprovidesseveralthread-safecollectionclassesthatshouldbeusedinplaceofthecorrespondingtypesintheSystem.CollectionsandSystem.Collections.Genericnamespaceswhenevermultiplethreadsareaccessingthecollectionconcurrentl

c# - 具有泛型的对象类型中的 "apostrophe + number"是什么意思(例如 "Collection` 1")?

我有一个对象(MyObject)和一个属性(MyProperty)。我想获取它的类型名称(即String或MyClass等)。我使用:PropertyInfopropInfo=typeof(MyObject).GetProperty("MyProperty");Console.WriteLine(propInfo.PropertyType.Name);Console.WriteLine(propInfo.PropertyType.FullName);简单类型没问题,但当MyProperty是通用类型,我在获取它的名称时遇到问题(例如Collection)。它打印:Collection`

c# - 为什么我不能将 List<List<Foo>> 传递给 IEnumerable<IEnumerable<Foo>>

此代码产生两个编译时错误:privatevoidDoSomething(){List>myFoos=GetFoos();UseFoos(myFoos);}privatevoidUseFoos(IEnumerable>){}Thebestoverloadedmethodmatchfor'NameSpace.Class.UseFoos(System.Collections.Generic.IEnumerable>)'hassomeinvalidarguments和Argument1:cannotconvertfrom'System.Collections.Generic.List>'to'

c# - 检查 Collection 中的所有项目是否具有相同的值

名为MeasurementCollection的集合的扩展方法检查每个项目的属性Template.Frequency(Enum)是否具有相同的值。publicstaticboolIsQuantized(thisMeasurementCollectionitems){return(fromiinitemsselecti.Template.Frequency).Distinct().Count()==1;}编辑关于底层类的信息MeasurementCollection:ICollectionIMeasurement{IMeasurementTemplateTemplate{get;}...

c# - 等于 System.Collections.Generic.List<T>... 的方法?

我正在创建一个派生自List的类...publicclassMyList:List{}我已经覆盖了MyListItem的Equals...publicoverrideboolEquals(objectobj){MyListItemli=objasMyListItem;return(ID==li.ID);//IDisapropertyofMyListItem}我也想在MyList对象中有一个Equals方法,它将比较列表中的每个项目,在每个MyListItem对象上调用Equals()。简单地调用...会很好MyListl1=newMyList(){newMyListItem(1),ne

c# - 可以锁定 System.Collections.Generic.List<t> 吗?

我一直在阅读有关syncroot元素的信息,但我在List类型中找不到它。那么System.Collections.Generic.List类型应该如何进行多线程同步呢? 最佳答案 你找不到它的原因是因为它是explicitlyremoved.如果它真的是你想做的,使用SynchronizedCollection或者创建一个专用的同步对象。最好的方法(通常)是创建一个专用的同步对象,正如Winston所说明的那样。SyncRoot的本质问题特性是它提供了一种错误的安全感——它只能处理非常有限的情况。开发人员经常忽略整个逻辑操作的同步

c# - LINQ: ...Where(x => x.Contains(以 "foo"开头的字符串 ))

给定以下类的集合:publicclassPost{...publicIListTags{get;set;}}有没有一种简单的方法可以使用LINQ获取所有包含以“foo”开头的标签的Post?varposts=newList{newPost{Tags=new[]{"fooTag","tag"}},newPost{Tags=new[]{"barTag","anyTag"}},newPost{Tags=new[]{"someTag","fooBarTag"}}};varpostsWithFooTag=posts.Where(x=>[somefancyLINQqueryhere]);posts

c# - Xunit.Assert.Collection 中的问题 - C#

我有一个类库,它包含以下模型和方法型号:publicclassEmployee{publicintEmpId{get;set;}publicstringName{get;set;}}方法:publicclassEmployeeService{publicListGetEmployee(){returnnewList(){newEmployee(){EmpId=1,Name="John"},newEmployee(){EmpId=2,Name="AlbertJohn"},newEmployee(){EmpId=3,Name="Emma"},}.Where(m=>m.Name.Contai

c# - Collection<T> 是封装 IList<T> 还是枚举 IList<T>?

如果我通过Collection属性公开内部成员:publicCollectionEntries{get{returnnewCollection(this.fieldImplimentingIList);}}当这个属性被调用时会发生什么?例如,调用以下代码行时会发生什么:Ttest=instanceOfAbove.Entries[i];instanceOfAbove[i]=valueOfTypeT;很明显,每次调用此属性时都会创建一个新的引用类型,但实际发生了什么?它只是包装IList吗?在下面,它是否枚举了IList并创建一个新的Collection实例?如果在for中使用此属性,我会