在List
上操作时,.All()
和.TrueForAll()
之间是否存在实际差异?我知道.All()
是IEnumerable
的一部分,所以为什么要添加.TrueForAll()
?
What is the use of default keyword in C#? Is it introduced in C# 3.0 ?
在List
上操作时,.All()
和.TrueForAll()
之间是否存在实际差异?我知道.All()
是IEnumerable
的一部分,所以为什么要添加.TrueForAll()
?
从用于List<;T>;.TrueForAll
的文档:
支持范围:4、3.5、3.0、2.0
所以它是在Enumerable.All
之前添加的。
对于一堆其他List<;T>
方法,其工作方式与LINQ方法类似。请注意,ConvertAll
有些不同,因为它具有知道正在处理List<;T>
并创建List<;T结果>
,这样它就可以预先分配所需的任何内容。
TrueForAll
在LINQ出现在.NET 3.5之前就存在于.NET 2.0中。
请参阅:http://msdn.microsoft.com/en-us/library/kdxe4x4w(v=VS.80).aspx
很抱歉把这个挖出来,但我遇到了这个问题,并看到关于差异的实际问题没有得到正确的回答。
不同之处在于:
IEnumerable.All()
extension method does an additional check for the extended object (in case it is null, it throws an exception).IEnumerable.All()
may not check elements in order. In theory, it would be allowed by specification to have the items to check in a different order every call. List.TrueForAll()
specifies in the documentation that it will always be in the order of the list.第二点是因为Enumerable.All
必须使用foreach
或MoveNext()
方法来迭代项,而List.TrueForAll()
内部使用带有列表索引的for
循环。
然而,您可以非常确定的是,foreach
/MoveNext()
方法也将按列表条目的顺序返回元素,因为许多程序都希望这样,如果将来更改,就会中断。
从性能的角度来看,List.TrueForAll()
应该更快,因为它少了一次检查,并且列表上的比
foreach
便宜。然而,通常编译器做得很好,并在这里进行了大量优化,因此最终可能(几乎)没有可测量的差异。
结论:List.TrueForAll()
在理论上是更好的选择。但实际上,这并没有什么区别。
TrueForAll
似乎特定于List,而All
是LINQ的一部分。
我的猜测是,前者可以追溯到.NET 2天,而后者是.NET 3.5中的新版本。
基本上,因为这种方法在林之前就已经存在了。TrueForAll on a List源于Framework 2.0。
TrueForAll
不是一个扩展方法,并且在版本2的框架中。
What is the use of default keyword in C#? Is it introduced in C# 3.0 ?
I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...
I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...
I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...
I have two EF entities. One has a property called HouseNumber. The other has two properties, one called StartHouseNumber and one called EndHouseNumber. I want to create a many to many association ...
How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...
Since I cannot order my dictionary, what is the best way of going about taking key value pairs and also maintaing an index?
Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...