随便玩玩之C# 11 程序控制-循环foreach
随便玩玩之C# 11 程序控制-循环foreach
1.foreach
foreach循环可以遍历集合中的所有项。语法如下:
1 2 3 4 | foreach (类型 变量名 in 集合对象) { 语句块(循环体) } |
所谓遍历,即将对象(集合)中的所有内容全部输出。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | using System; using System.Collections.Generic; namespace ForeachExpression { internal class Program { static void Main( string [] args) { //创建一个数组 int [] LotteryNumber = { 06, 12, 26, 28, 29, 32, 15 }; //遍历数组。把数据中的每个数单独输出 foreach ( int num in LotteryNumber) { Console.WriteLine(num); } Console.WriteLine( "---------------------------" ); // 创建一个字典,并向字典内添加元素(数据) // 字典键值对(Key,Value),姓名、成绩。 Dictionary< string , int > MathResult = new Dictionary< string , int >(); MathResult.Add( "张三" , 88); MathResult.Add( "李四" , 85); MathResult.Add( "王五" , 90); MathResult.Add( "赵六" , 78); // 遍历字典,从字典中输出某个人的数学成绩。 // 一次循环输出一个人的信息,相当于表格中的一行 foreach ( var SomeBodyMathResult in MathResult) { //字典中的键 Console.Write(SomeBodyMathResult.Key); //字典中键的关联值 Console.WriteLine(SomeBodyMathResult.Value); } Console.WriteLine( "---------------------------" ); // 创建一个匿名对象并填充一些数据。 // 姓名,语文,数学,英语成绩。 var Result = new [] { new { name = "张三" , chinese = 96,math=88,english=90 }, new { name = "李四" , chinese = 93,math=90,english=93 }, new { name = "王五" , chinese = 98,math=70,english=85 }, new { name = "赵六" , chinese = 92,math=85,english=88 } }; // 遍历对象,一次循环输出一个人的信息,相当于表格中的一行 foreach ( var SomeBodyResult in Result) { Console.WriteLine(SomeBodyResult.name); Console.WriteLine(SomeBodyResult.chinese); Console.WriteLine(SomeBodyResult.math); Console.WriteLine(SomeBodyResult.english); } Console.WriteLine( "---------------------------" ); Console.ReadKey(); } } } |
运行结果。
2.WPF中的DataGrid控件
DataGrid控件是一个数据表格控件。
新建一个WPF项目,解决方案命名为WPFDataGrid。
将工具箱中的Button控件拖放到WPF设计器的空白区域。选中WPF设计器中Button控件,将其Content属性修改为确定。
将工具箱中的DataGrid空间拖放到WPF设计器的空白区域。调整好DataGrid空间的外观大小,将其名称修改为ResultTable。
双击"确定"按钮,WPF转到代码编辑器里,在Button_Click()方法中输入循环代码。
1 2 3 4 5 6 7 8 9 10 11 | private void Button_Click( object sender, RoutedEventArgs e) { var Result = new [] { new { name = "张三" , chinese = 96,math=88,english=90 }, new { name = "李四" , chinese = 93,math=90,english=93 }, new { name = "王五" , chinese = 98,math=70,english=85 }, new { name = "赵六" , chinese = 92,math=85,english=88 } }; ResultTable.ItemsSource = Result; } |
运行结果。
运行程序,点击确定。匿名对象中的数据填充到名为ResultTable的DataGrid控件中。
参考资料:
1.foreach
https://learn.microsoft.com/zh-cn/dotnet/csharp/language-reference/statements/iteration-statements
2.datagrid
https://learn.microsoft.com/zh-cn/dotnet/desktop/wpf/controls/datagrid
3.匿名类型
https://learn.microsoft.com/zh-cn/dotnet/csharp/fundamentals/types/anonymous-types
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具