官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 囊谦县| 惠安县| 招远市| 房产| 乌兰县| 钦州市| 睢宁县| 隆化县| 汝南县| 扬中市| 普洱| 江川县| 黄梅县| 穆棱市| 古浪县| 厦门市| 来安县| 尼玛县| 仪征市| 汪清县| 天峨县| 杭锦后旗| 白河县| 昌宁县| 景谷| 鹰潭市| 鸡东县| 丽水市| 吐鲁番市| 宝丰县| 邻水| 杂多县| 龙南县| 肇东市| 城口县| 阿克陶县| 海淀区| 太原市| 台湾省| 临颍县| 龙游县|