首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

如何在不使用c#中的迭代器的情况下从列表中获取数据

在不使用C#中的迭代器的情况下,您可以使用以下方法从列表中获取数据:

  1. 使用for循环:List<int> myList = new List<int> { 1, 2, 3, 4, 5 }; for (int i = 0; i < myList.Count; i++) { Console.WriteLine(myList[i]); }
  2. 使用foreach循环:List<int> myList = new List<int> { 1, 2, 3, 4, 5 }; foreach (int item in myList) { Console.WriteLine(item); }
  3. 使用LINQ查询:List<int> myList = new List<int> { 1, 2, 3, 4, 5 }; var query = from item in myList select item; foreach (int item in query) { Console.WriteLine(item); }
  4. 使用扩展方法:public static class Extensions { public static void ForEach<T>(this IEnumerable<T> source, Action<T> action) { foreach (T item in source) { action(item); } } } List<int> myList = new List<int> { 1, 2, 3, 4, 5 }; myList.ForEach(item => Console.WriteLine(item));

以上方法都可以在不使用迭代器的情况下从列表中获取数据。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

领券