在C#中使用lambda表达式或匿名方法时,我们必须警惕对修改后的闭包陷阱的访问.例如:
foreach (var s in strings)
{
query = query.Where(i => i.Prop == s); // access to modified closure
...
}
Run Code Online (Sandbox Code Playgroud)
由于修改后的闭包,上面的代码将导致Where查询中的所有子句都基于最终值s.
正如这里所解释的那样,这是因为上面循环中s声明的变量foreach在编译器中被翻译成这样:
string s;
while (enumerator.MoveNext())
{
s = enumerator.Current;
...
}
Run Code Online (Sandbox Code Playgroud)
而不是像这样:
while (enumerator.MoveNext())
{
string s;
s = enumerator.Current;
...
}
Run Code Online (Sandbox Code Playgroud)
正如这里所指出的,在循环外声明变量没有性能优势,在正常情况下,我能想到这样做的唯一原因是你计划在循环范围之外使用变量:
string s;
while (enumerator.MoveNext())
{
s = enumerator.Current;
...
}
var finalString = s;
Run Code Online (Sandbox Code Playgroud)
但是,foreach循环中定义的变量不能在循环外使用: …
Dictionary<T1,T2>在C#中合并2个或更多字典()的最佳方法是什么?(像LINQ这样的3.0功能很好).
我正在考虑一种方法签名:
public static Dictionary<TKey,TValue>
Merge<TKey,TValue>(Dictionary<TKey,TValue>[] dictionaries);
Run Code Online (Sandbox Code Playgroud)
要么
public static Dictionary<TKey,TValue>
Merge<TKey,TValue>(IEnumerable<Dictionary<TKey,TValue>> dictionaries);
Run Code Online (Sandbox Code Playgroud)
编辑:从JaredPar和Jon Skeet得到一个很酷的解决方案,但我正在考虑处理重复键的东西.在发生碰撞的情况下,只要它是一致的,将哪个值保存到dict并不重要.
哪个代码段会提供更好的性能?以下代码段是用C#编写的.
1.
for(int counter=0; counter<list.Count; counter++)
{
list[counter].DoSomething();
}
Run Code Online (Sandbox Code Playgroud)
2.
foreach(MyType current in list)
{
current.DoSomething();
}
Run Code Online (Sandbox Code Playgroud) 我有一个Enumerable<T>并且正在寻找一种方法,允许我为每个元素执行一个动作,有点像Select然后是副作用.就像是:
string[] Names = ...;
Names.each(s => Console.Writeline(s));
Run Code Online (Sandbox Code Playgroud)
要么
Names.each(s => GenHTMLOutput(s));
// (where GenHTMLOutput cannot for some reason receive the enumerable itself as a parameter)
Run Code Online (Sandbox Code Playgroud)
我试过了Select(s=> { Console.WriteLine(s); return s; }),但它没有打印任何东西.
我想知道为什么List<T>.ForEach(Action<T>)存在.
这样做有什么好处/差别:
elements.ForEach(delegate(Element element){ element.DoSomething(); });
Run Code Online (Sandbox Code Playgroud)
过度
foreach(Element element in elements) { element.DoSomething();}
Run Code Online (Sandbox Code Playgroud)
?
使用foreach循环或ForEach LINQ方法之间是否存在任何差异(性能或其他)?
对于上下文,这是我的一个方法的一部分:
foreach (var property in typeof(Person).GetProperties())
{
Validate(property.Name);
}
Run Code Online (Sandbox Code Playgroud)
我也可以使用此代码执行相同的任务:
typeof(Person)
.GetProperties()
.ToList()
.ForEach(property => Validate(property.Name));
Run Code Online (Sandbox Code Playgroud)
什么时候使用循环结构比使用方法链更好?
这是我使用该ForEach方法的另一个例子,但可以轻松使用foreach循环和变量:
// LINQ
PrivateData.Database.Users
.Cast<User>()
.Where(user => user.LoginType == LoginType.WindowsUser)
.Select(user => new { Name = user.Name, Login = user.Login })
.ToList()
.ForEach(result => WriteObject(result));
// Loop
var users = PrivateData.Database.Users
.Cast<User>()
.Where(user => user.LoginType == LoginType.WindowsUser)
.Select(user => new { Name = user.Name, Login = user.Login });
foreach(var user in users)
{ …Run Code Online (Sandbox Code Playgroud) 我正在使用代码库,其中列表需要经常搜索单个元素.
使用Predicate和Find()比在List上手动执行枚举更快吗?
例如:
string needle = "example";
FooObj result = _list.Find(delegate(FooObj foo) {
return foo.Name == needle;
});
Run Code Online (Sandbox Code Playgroud)
与
string needle = "example";
foreach (FooObj foo in _list)
{
if (foo.Name == needle)
return foo;
}
Run Code Online (Sandbox Code Playgroud)
虽然它们在功能上是等同的,但它们在性能方面是否相同?
什么被认为是Java 8中Collection的惯用迭代,为什么?
for (String foo : foos) {
String bar = bars.get(foo);
if (bar != null)
System.out.println(foo);
}
Run Code Online (Sandbox Code Playgroud)
要么
foos.forEach(foo -> {
String bar = bars.get(foo);
if (bar != null)
System.out.println(foo);
});
Run Code Online (Sandbox Code Playgroud) 我有一个IEnumerable<>对象,我正在使用 for 循环进行迭代,如下所示:
IEnumerable<Process> dataHolder = GetData();
// how to write below foreach loop in one-liner?
foreach (var x in dataHolder)
{
loggingService.Warn(LOG_TITLE, "Value: " + x.ClientName);
}
Run Code Online (Sandbox Code Playgroud)
我只是想知道是否有任何方法可以foreach在one-liner.
foreach (IndexingField field in document.IndexingFields)
{
if (field.TableColumn.ToLower() == "esig"&& field.Data != null && !string.IsNullOrEmpty(field.Data.ToString()) && field.Data.ToString() != "N")
{
EsigBase64Value = field.Data.ToString();
field.Data = "Y";
}
}
Run Code Online (Sandbox Code Playgroud)
这是我正在使用的每个循环,我想将每个循环的这个转换为 lambda 表达式,请帮助我