Tag: generic list

.Net – 什么时候是List .ForEach优先于标准的foreach循环?

通用列表类具有.ForEach(Action action)方法。 现在我已经完成了一些关于它们如何表现的简单时间,似乎通用的ForEach是较差的表演者。 (Snippet Compiler Friendly)代码如下 – public static class timer{ public static long foreachloop = 0; public static long Gforeachloop = 0;} public class something{ public List myStrings = new List(); public something() { for(int i = 1; i<=5000000;i++) { myStrings.Add(i.ToString()); } }} public class cls1{ private static List Strings = new List(); private […]

将一种类型的列表转换为C#3.5中另一种类型的列表

我有一个具有ID和Name属性的对象。 我有一个对象列表,但我想将其转换为包含对象名称的字符串列表。 我猜测有一些花哨的Linq方法会将对象的名称放入列表中。 List myObjectNames = myObjectList.?

通用列表为IEnumerable

我正在尝试将List转换为IEnumerable,因此我可以validation不同的列表不为null或为空: 假设myList是List 。 然后在我想要的来电代码中: Validator.VerifyNotNullOrEmpty(myList as IEnumerable, @”myList”, @”ClassName.MethodName”); 有价值的代码是: public static void VerifyNotNullOrEmpty(IEnumerable theIEnumerable, string theIEnumerableName, string theVerifyingPosition) { string errMsg = theVerifyingPosition + ” ” + theIEnumerableName; if (theIEnumerable == null) { errMsg += @” is null”; Debug.Assert(false); throw new ApplicationException(errMsg); } else if (theIEnumerable.Count() == 0) { errMsg += @” is empty”; […]

如何使自定义线程安全通用列表返回C#中的整个列表?

我是一个线程菜鸟,我试图在C#(.NET 3.5 SP1)中编写一个自定义线程安全通用列表类。 我已经阅读了为什么线程安全集合如此困难? 。 在查看了课程的要求之后,我想我只需要安全地添加到列表中并返回列表。 该示例显示了我想要的所有内容,除了它缺少返回列表方法,因此我编写了自己的公共方法,如下所示: 更新:根据给出的建议,我已经审查了我的要求,因此将课程简化为如下: public sealed class ThreadSafeList { private readonly IList list = new List(); private readonly object lockable = new object(); public void Add(T t) { lock (lockable) { list.Add(t); } } public IList GetSnapshot() { IList result; lock (lockable) { result = new List(list); } return result; } […]

替换对象列表中的对象

在C#中,如果我有一个List ,并且我有一个T类型的对象,我如何用类型T的对象替换List的特定项? 这是我尝试过的: List customListItems = new List(); CustomListItem customListItem1 = new CustomListItem() { name = “Item 1”, date = DateTime.MinValue}; CustomListItem customListItem2 = new CustomListItem() { name = “Item 2”, date = DateTime.MinValue }; CustomListItem customListItem3 = new CustomListItem() { name = “Item 3”, date = DateTime.MinValue }; customListItems.Add(customListItem1); customListItems.Add(customListItem2); customListItems.Add(customListItem3); CustomListItem newCustomListItem = […]

当找不到值时,C#List .BinarySearch返回值

如果项目不存在,我对List的BinarySearch方法感到困惑。 我有 List theList = {1, 3, 5, …}. 如预期的那样, theList.BInarySearch(0)返回0,而theList.BInarySearch(3)返回1。 但是, theList.BinarySearch(1)返回-2 ,而不是-1,正如我所期望的那样。 MSDN手册说:“返回值:如果找到项目,则排序列表中项目的从零开始的索引;否则,是负数,它是下一个元素的索引的按位补码,大于项目或,如果没有更大的元素,则为Count的按位补码。“ 一个“按位补码”? 我在这里错过了什么,为什么theList.BinarySearch(1) != -1 ?

为什么IList 实现IEnumerable 和ICollection 而ICollection 本身实现IEnumerable

为什么IList这样定义? public interface IList : ICollection, IEnumerable, IEnumerable public interface ICollection : IEnumerable, IEnumerable public interface IEnumerable : IEnumerable 不可能只是 public interface IList : ICollection 所以,为了测试我创建了这些接口,只是为了确定它是否有效! public interface IOne { string One(); } public interface ITwo : IOne { string Two(); } public interface IThree : ITwo, IOne { string Three(); } 虽然完美无瑕,但Resharper抱怨“冗余接口”。 有什么想法为什么微软继续这个实现?

构建linq查询以获得最快性能的正确方法?

这里也提出了类似的问题,但没有一个符合我的需要。 我做了测试用例,看看哪个更快。 但我觉得我的linq代码仍然很慢。 如何构建linq代码以获得更快的性能? 其他人说使用double .Tolist()会导致操作变慢,当我测试它时,它表明它比任何其他测试都快。 测试: Preparation ————————————————————— return Properties of UserInfo(userinf, true){ UserID = userinf.UserID; FirstName = userinf.user.FirstName; MiddleName = userinf.user.MiddleName; LastName = userinf.user.LastName; LoginID = userinf.user.LoginID; Birthday = userinf.Birthday; } skip = 0; take = 100; total table records = 304; Linq to Entity Framework 提琴手:v2.4.0.0 https://127.0.0.1/…./RetrieveUserInfo?skip=0&take=100 { “client”:{ “SessionID”:”5433ab64-7e0d-444f-b886-a901ea9a0601″ }, “session”:{ […]

什么是“现代”的方式来找到两个列表对象中的常见项目?

我有两个包含不同类型的通用列表,例如,我们称之为Products和Employees 。 我正在尝试找到与Employees位于同一位置的product.SiteId == emp.SiteId ,即product.SiteId == emp.SiteId List lstProds; List lstEmps; 我的(老skool)大脑告诉我使用forEach循环找到匹配但我怀疑有一个(’更好’/更快/更快?)的方式来使用Linq。 任何人都可以照亮我吗? 我在网上找到的所有例子都涉及原语列表(字符串/整数),并没有特别的帮助。

通用接口的通用列表是不允许的,任何替代方法?

我试图找到使用通用接口通用列表作为变量的正确方法。 这是一个例子。 它可能不是最好的,但希望你能明白这一点: public interface IPrimitive { T Value { get; } } 然后在另一个类中,我希望能够声明一个变量,该变量包含对任意T实现IPrimitive的对象列表。 // I know this line will not compile because I do not define T List<IPrimitive> primitives = new List<IPrimitives>; primitives.Add(new Star()); // Assuming Star implements IPrimitive primitives.Add(new Sun()); // Assuming Sun implements IPrimitive 请注意,对于列表中的每个条目, IPrimitive可能不同。 关于如何建立这种关系的任何想法? 替代方法?