keyvaluepair<T,T>是一种集合类型吗,网上有的说是一种结构,他怎么感觉像一个泛型类。

解决方案 »

  1.   

    不是,它只是一个泛型类。再比如 IComparer<T>、Func<T> 等等,都和集合没有任何关系。
      

  2.   

    呵呵,我是用这个keyvaluepair来foreach字典。网上说是一种结构,是不是说是结构体的意思,又不像结构体。为什么说是一种结构呢?
      

  3.   

    http://msdn.microsoft.com/zh-cn/library/5tbh8a42(v=vs.80).aspx
    是结构体。
      

  4.   

    为了加深你的理解,你可以自己实现一个类似的结构体,看下面的代码:
    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);
                }
            }
        }
    }