- Beginning C# 7 Hands-On:Advanced Language Features
- Tom Owsiak
- 258字
- 2021-07-02 15:29:22
Iterating over key-value pairs
Now, imagine that we want to iterate over these keys, because we potentially have many of them. One way to do this is as follows:
foreach(KeyValuePair<string, string> kvp in filePrograms)
You can iterate over key-value pairs like this. Next, enter the following between a set of curly braces beneath the preceding line:
Process.Start(kvp.Key, kvp.Value);
Here, after Process.start, you display the key and the values. So, you can say kvp.key, which is a property of the key-value pair and kvp.value is a property of the key-value pair as well.
In a realistic application, where a program could be missing or something else could happen, it would be better to put this in a TryCatch block and so on, but for our purposes, this is sufficient and it keeps it short.
If you want, you can also iterate over individual keys and values and so on. So, as another example, you can do something like the following beneath the closed curly brace under the preceding line:
foreach(string key in filePrograms.Keys)
To get the individual keys, you type the name of the dictionary and then the name of the keys collection, Keys, which appears in the popup.
This is how you can access just the keys. If you want to display them, you definitely can; to do this, enter the following between a set of curly braces under this line:
sampLabel.Txt += $"<br>{key}";
To show the key, you type {key}. Remember to insert the <br> tag, the += operator to append, the $ symbol, and close with a semicolon.
- DB2 V9權(quán)威指南
- Vue.js設(shè)計與實現(xiàn)
- 觸·心:DT時代的大數(shù)據(jù)精準(zhǔn)營銷
- Oracle Exadata性能優(yōu)化
- Building Modern Web Applications Using Angular
- Python數(shù)據(jù)可視化:基于Bokeh的可視化繪圖
- C++ Builder 6.0下OpenGL編程技術(shù)
- Spring Boot+Spring Cloud+Vue+Element項目實戰(zhàn):手把手教你開發(fā)權(quán)限管理系統(tǒng)
- Java程序員面試算法寶典
- 深入淺出Windows API程序設(shè)計:編程基礎(chǔ)篇
- 基于免疫進化的算法及應(yīng)用研究
- UI智能化與前端智能化:工程技術(shù)、實現(xiàn)方法與編程思想
- INSTANT CakePHP Starter
- Python數(shù)據(jù)結(jié)構(gòu)與算法(視頻教學(xué)版)
- ASP.NET Core 2 Fundamentals