显然有很多方法可以迭代集合.好奇,如果有任何差异,或为什么你使用一种方式而不是另一种方式.
第一种:
List<string> someList = <some way to init>
foreach(string s in someList) {
<process the string>
}
Run Code Online (Sandbox Code Playgroud)
另一种方式:
List<string> someList = <some way to init>
someList.ForEach(delegate(string s) {
<process the string>
});
Run Code Online (Sandbox Code Playgroud)
我认为,除了我上面使用的匿名代表之外,我还有一个你可以指定的可重用代理......
我是.NET的新手,曾经在PHP工作.我需要通过foreach对象字典迭代.我的设置是一个MVC4应用程序.
模型看起来像这样:
public class TestModels
{
Dictionary<int, dynamic> sp = new Dictionary<int, dynamic>
{
{1, new {name="abc", age="1"}},
{2, new {name="def", age="2"}}
}
}
Run Code Online (Sandbox Code Playgroud)
控制器:
public class TestController : Controller
{
Models.TestModels obj = new Models.TestModels();
}
Run Code Online (Sandbox Code Playgroud)
如何循环访问obj对象并检索字典的值并在视图中打印它们?
在C#7中是否可以在字典中的foreach循环中使用解构?像这样的东西:
var dic = new Dictionary<string, int>{ ["Bob"] = 32, ["Alice"] = 17 };
foreach (var (name, age) in dic)
{
Console.WriteLine($"{name} is {age} years old.");
}
Run Code Online (Sandbox Code Playgroud)
它似乎不适用于Visual Studio 2017 RC4和.NET Framework 4.6.2:
错误CS1061:'KeyValuePair'不包含'Deconstruct'的定义,并且没有扩展方法'Deconstruct'可以找到接受类型'KeyValuePair'的第一个参数(你是否缺少using指令或汇编引用?)
我通常使用foreach循环遍历Dictionary.
Dictionary<string, string> dictSummary = new Dictionary<string, string>();
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我想修剪空白的条目,而foreach循环确实不允许这样做.
foreach (var kvp in dictSummary)
{
kvp.Value = kvp.Value.Trim();
}
Run Code Online (Sandbox Code Playgroud)
如何使用for循环执行此操作?
for (int i = dictSummary.Count - 1; i >= 0; i--)
{
}
Run Code Online (Sandbox Code Playgroud) var dict = new Dictionary<int, string>();
for (int i = 0; i < 200000; i++)
dict[i] = "test " + i;
Run Code Online (Sandbox Code Playgroud)
我使用下面的代码迭代这本字典:
foreach (var pair in dict)
Console.WriteLine(pair.Value);
Run Code Online (Sandbox Code Playgroud)
然后,我用这个迭代它:
foreach (var key in dict.Keys)
Console.WriteLine(dict[key]);
Run Code Online (Sandbox Code Playgroud)
第二次迭代减少了约3秒.我可以通过两种方法获得键和值.我想知道第二种方法是否有缺点.由于我能找到的评价最高的问题不包括这种迭代字典的方式,我想知道为什么没有人使用它以及它如何更快地工作.
我有一个字典,其中我需要迭代并进行更改.我不能使用foreach语句,因为它有时会抛出InvalidOperationException,说在收集期间无法修改集合.
我可以使用for循环,结合Dictionary.ElementAt方法,并且我在其他类中成功使用它,但是在这个特定的类中,无法找到方法ElementAt!有任何想法吗?
我有一个:
var selectedDates = new Dictionary<string, string>();
selectedDates.Add("2014-06-21", DateTime.Now.ToLongDateString());
selectedDates.Add("2014-07-21", DateTime.Now.AddDays(5).ToLongDateString());
selectedDates.Add("2014-08-21", DateTime.Now.AddDays(9).ToLongDateString());
selectedDates.Add("2014-09-21", DateTime.Now.AddDays(14).ToLongDateString());
Run Code Online (Sandbox Code Playgroud)
如何在不知道密钥的情况下循环项目?
例如,我想得到项[0]的值
如果我做:
var item = selectedDates[0].value; // I get an error
Run Code Online (Sandbox Code Playgroud) 我喜欢列表,因为它们非常易于使用和管理.但是,我需要一个包含多个元素的列表,比如记录.
我是C#的新手,非常感谢所有帮助!(Stackoverflow晃动!)
考虑一个单个元素列表的简单实用示例,它非常有用:
public static List<string> GetCities()
{
List<string> cities = new List<string>();
cities.Add("Istanbul");
cities.Add("Athens");
cities.Add("Sofia");
return cities;
}
Run Code Online (Sandbox Code Playgroud)
如果我希望列表为每条记录都有两个属性,我该怎么做?(作为一个数组?)
例如,这个伪代码的真实代码是什么?:
public static List<string[2]> GetCities()
{
List<string> cities = new List<string>();
cities.Name,Country.Add("Istanbul","Turkey");
cities.Name,Country.Add("Athens","Greece");
cities.Name,Country.Add("Sofia","Bulgaria");
return cities;
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
我正在将一个项目从 Java 转换为 C#。我试图搜索这个,但我遇到的只是关于枚举的问题。有一个 Hashtable htPlaylist,循环使用 Enumeration 遍历键。我如何将此代码转换为 C#,但使用字典而不是哈希表?
// My C# Dictionary, formerly a Java Hashtable.
Dictionary<int, SongInfo> htPlaylist = MySongs.getSongs();
// Original Java code trying to convert to C# using a Dictionary.
for(Enumeration<Integer> e = htPlaylist.keys(); e.hasMoreElements();
{
// What would nextElement() be in a Dictonary?
SongInfo popularSongs = htPlaylist.get(e.nextElement());
}
Run Code Online (Sandbox Code Playgroud) IDictionary如果我不知道其中的键和值的具体类型,我将如何遍历键和值的值,因此只想将它们视为objects?
如果我这样做:
foreach(var x in myIDictionary) { ... }
我认为x是一个object.但我怎么会得到Key与Value出它(这两个类型为objectS),在指定的IDictionaryEnumerator?有IKeyValuePair没有通用参数吗?
我想我可以手动循环使用枚举器MoveNext等,但我觉得必须有办法做到这一点foreach!
c# ×9
dictionary ×3
.net ×2
idictionary ×2
c#-7.0 ×1
enumeration ×1
for-loop ×1
generics ×1
iteration ×1
java ×1
loops ×1