草庐IT

generic-list

全部标签

C# 不寻常的继承语法 w/generics

我在NHibernate类定义中偶然发现了这一点:publicclassSQLiteConfiguration:PersistenceConfiguration所以这个类继承自一个基类,该基类由...派生类参数化?我的头爆炸了。谁能解释一下这意味着什么以及这种模式有何用处?(顺便说一下,这不是一个特定于NHibernate的问题。) 最佳答案 That'safunnyCuriouslyRecurringTemplatePattern,isn'tit? 关于C#不寻常的继承语法w/gene

c# - List<Task> - 使用 C# Entity Framework 的 UPSERT 数据库记录

我有一个Employee对象,我正在尝试使用单个DB实体上下文使用多个任务(并行执行)来更新记录(即更新/删除)。但是我收到以下异常Message="Objectreferencenotsettoaninstanceofanobject."考虑以下DTOpublicclassEmployee{publicintEmployeeId{get;set;}publicstringFirstName{get;set;}publicstringLastName{get;set;}publicListContactPhoneNumbers{get;set;}publicListContactEma

c# - List<T>.Enumerator 的 Reset 方法的行为

以下两种方法(一种使用IEnumerator,另一种使用List.Enumerator)即使看起来相同会产生不同的结果。staticvoidM1(){varlist=newList(){1,2,3,4};IEnumeratoriterator=list.GetEnumerator();while(iterator.MoveNext()){Console.Write(iterator.Current);}iterator.Reset();while(iterator.MoveNext()){Console.Write(iterator.Current);}}staticvoidM2(){

c# - 将带有 List 的字典转换为 IEnumerable

我有一本字典:Dictionary>test=newDictionary>();然后我填充这个字典,因此我需要这个列表以便我可以调用Add()。我的问题是函数需要返回:Dictionary>有没有简单的方法可以做到这一点,而无需执行明显的操作并循环遍历我的原始字典并手动执行? 最佳答案 returndictionary.ToDictionary(x=>x.Key,x=>x.Value.AsEnumerable()) 关于c#-将带有List的字典转换为IEnumerable,我们在Sta

c# - IQueryable<T> 给出的结果与 List<T> 不同

如果我在我的EntityFramework结果中对IQueryable使用Select,我将得到4个项目。如果我在IQueryable.ToList()上使用Select,我会得到所有36个项目。函数代码如下:publicImagesGetModelViewGet(intstart,intcount){if(countImageHandler.MaxResult)count=ImageHandler.MaxResult;IQueryableimagesList=ImagesHandler.FetchRangeScore(start,count).Where(m=>m.Domain==D

c# - List<T>.Last() 是否枚举集合?

考虑List的边界是已知的,确实.Last()枚举集合?我问这个是因为documentation说它是由Enumerable定义的(在这种情况下,它将需要枚举集合)如果它确实枚举了集合,那么我可以简单地通过索引访问最后一个元素(正如我们所知的.Count的List),但必须这样做似乎很愚蠢。... 最佳答案 如果集合是IEnumerable,它会枚举集合而不是IList(对于数组或列表,将使用索引)。Enumerable.Last通过以下方式实现(ILSpy):publicstaticTSourceLast(thisIEnumera

c# - 返回 List<T> 的只读包装器的哪种方式更可取?

比如说,我们有一个带有私有(private)列表的通用类。我们至少可以通过两种方式让它返回此列表的只读包装器:publicclassTest{publicListlist=newList();publicIEnumerableValues1{get{foreach(Tiinlist)yieldreturni;}}publicIEnumerableValues2{get{returnlist.AsReadOnly();}}}Values1和Values2都反射(reflect)了底层集合中的任何变化,并防止它通过自身进行修改。哪种方式更可取?应该注意什么?或者还有其他更好的方法吗?

c# - 在 c# 中设置/扩展 List<T> 长度

给定一个List在c#中有没有办法扩展它(在其能力范围内)并将新元素设置为null?我想要类似memset的东西.我不是在这里寻找糖,我想要快速代码。我知道在C中,操作可以在每个条目1-3个asm操作中完成。我找到的最佳解决方案是this:list.AddRange(Enumerable.Repeat(null,count-list.Count));但是这是c#3.0(首选我当前的代码使用:while(list.Count所以这是时间成本的起点。motivationforthis是我需要设置第n个元素,即使它在旧计数之后。 最佳答案

c# - 通用约束 : Can I test Equality of generic that can be a reference or value type?

我想要一个通用类,它可以接受引用类型或值类型,并且只执行基于相等性测试的操作。考虑以下几点:publicclassPropertywhereTProp:struct,IEquatable{publicTPropValue;publicvoidSetValue(ObservableObjectowner,TPropvalue){if(!Value.Equals(value))//cannotuse!=onstructconstrainedTProp{//...settheproperty}}}publicclassByRefPropertywhereTProp:class//Dontwa

c# - 检查两个 List<T> 是否相等的最快方法

我有两个列表ListA和ListB两者都有1000条记录。Emp是Employee类的一个对象。下面是我的Employee类publicclassEmployee{intID=0;stringName=String.Empty;stringDept=String.Empty;stringAddress=String.Empty;intAge=0;stringEmail=String.Empty;}我想验证两个列表是否相等。Emp对象可以按不同的顺序放置。此外,可能有多个Emp对象在两个列表中具有完全相同的信息。我也必须验证这些。我尝试对列表进行排序并使用SequenceEqual进行比