C# dictionary传参
时间: 2023-10-19 18:09:18 浏览: 308
在C#中,可以通过将Dictionary作为参数传递给方法来实现字典的传参。下面是一个示例:
```csharp
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
Dictionary<string, int> dict = new Dictionary<string, int>();
dict.Add("apple", 5);
dict.Add("banana", 3);
// 调用方法并传递字典作为参数
PrintDictionary(dict);
}
static void PrintDictionary(Dictionary<string, int> dict)
{
foreach (var kvp in dict)
{
Console.WriteLine($"Key: {kvp.Key}, Value: {kvp.Value}");
}
}
}
```
输出结果:
```
Key: apple, Value: 5
Key: banana, Value: 3
```
阅读全文