keyvaluepair<T,T>是一种集合类型吗 keyvaluepair<T,T>是一种集合类型吗,网上有的说是一种结构,他怎么感觉像一个泛型类。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 不是,它只是一个泛型类。再比如 IComparer<T>、Func<T> 等等,都和集合没有任何关系。 呵呵,我是用这个keyvaluepair来foreach字典。网上说是一种结构,是不是说是结构体的意思,又不像结构体。为什么说是一种结构呢? http://msdn.microsoft.com/zh-cn/library/5tbh8a42(v=vs.80).aspx是结构体。 为了加深你的理解,你可以自己实现一个类似的结构体,看下面的代码:using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication1{ struct MyKeyValuePair<TKey, TValue> { public TKey Key { get; set; } public TValue Value { get; set; } public MyKeyValuePair(TKey key, TValue value) : this() { Key = key; Value = value; } } static class DictionaryExtend { static public void Add<TKey, TValue>(this Dictionary<TKey, TValue> dict, MyKeyValuePair<TKey, TValue> item) { dict.Add(item.Key, item.Value); } } class Program { static void Main(string[] args) { Dictionary<int, string> dict = new Dictionary<int, string>(); dict.Add(new MyKeyValuePair<int, string>(1, "abc")); dict.Add(new MyKeyValuePair<int, string>(2, "def")); foreach (var item in dict) { Console.WriteLine("key: {0}, value {1}.", item.Key, item.Value); } } }} 基础问题,各位亲帮俺解答下呗,纯粹给分哦。。。 XML ImportNode问题 我写了一段代码在IE和Chrome中表现正常,在Firefox中表现不正常 请教listview小问题 关于winform的reportviewer打印问题 求助 页面绑定! 关于girdview编辑模板事件 请教一个在WEBFORM窗体中调用WINFORM窗体的问题? IIS后台多任务线程限制 如何在datagrid左边有一个数字自动编号 windows 窗口颜色怎么改变? 求一个坐标转换的源码
是结构体。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication1
{
struct MyKeyValuePair<TKey, TValue>
{
public TKey Key { get; set; }
public TValue Value { get; set; }
public MyKeyValuePair(TKey key, TValue value) : this()
{
Key = key;
Value = value;
}
} static class DictionaryExtend
{
static public void Add<TKey, TValue>(this Dictionary<TKey, TValue> dict, MyKeyValuePair<TKey, TValue> item)
{
dict.Add(item.Key, item.Value);
}
} class Program
{
static void Main(string[] args)
{
Dictionary<int, string> dict = new Dictionary<int, string>();
dict.Add(new MyKeyValuePair<int, string>(1, "abc"));
dict.Add(new MyKeyValuePair<int, string>(2, "def"));
foreach (var item in dict)
{
Console.WriteLine("key: {0}, value {1}.", item.Key, item.Value);
}
}
}
}