ASP.NET Cache 缓存 遍历 笔记

Cache遍历方法示例:

System.Web.Caching.Cache wxCache = new System.Web.Caching.Cache();
wxCache["a"] = "avlue";
wxCache["b"] = "bvlue";
wxCache["c"] = "cvlue";

foreach (System.Collections.DictionaryEntry item in wxCache)//foreach的Source必须是实现了IEnumerable接口的对象。接收wxCache遍历项的必须是DictionaryEntry类型,因为GetEnumerator()的返回类型是IDictionaryEnumerator
{
    Console.WriteLine($"key:{item.Key},value:{item.Value}");
}

C#方法说明:

System.Web.Caching.Cache实现了 IEnumerable接口,IEnumerable要求实现返回类型为IDictionaryEnumerator 的GetEnumerator方法。

System.Web.Caching.Cache的GetEnumerator方法定义:
//
// 摘要:
//     Exposes an enumerator, which supports a simple iteration over a non-generic collection.To
//     browse the .NET Framework source code for this type, see the Reference Source.
[ComVisible(true)]
[Guid("496B0ABE-CDEE-11d3-88E8-00902754C43A")]
public interface IEnumerable
{
    //
    // 摘要:
    //     Returns an enumerator that iterates through a collection.
    //
    // 返回结果:
    //     An System.Collections.IEnumerator object that can be used to iterate through
    //     the collection.
    [DispId(-4)]
    IEnumerator GetEnumerator();
}
返回类型IDictionaryEnumerator 的定义:
    //
    // 摘要:
    //     Enumerates the elements of a nongeneric dictionary.
    [ComVisible(true)]
    public interface IDictionaryEnumerator : IEnumerator
    {
        //
        // 摘要:
        //     Gets the key of the current dictionary entry.
        //
        // 返回结果:
        //     The key of the current element of the enumeration.
        //
        // 异常:
        //   T:System.InvalidOperationException:
        //     The System.Collections.IDictionaryEnumerator is positioned before the first entry
        //     of the dictionary or after the last entry.
        object Key { get; }
        //
        // 摘要:
        //     Gets the value of the current dictionary entry.
        //
        // 返回结果:
        //     The value of the current element of the enumeration.
        //
        // 异常:
        //   T:System.InvalidOperationException:
        //     The System.Collections.IDictionaryEnumerator is positioned before the first entry
        //     of the dictionary or after the last entry.
        object Value { get; }
        //
        // 摘要:
        //     Gets both the key and the value of the current dictionary entry.
        //
        // 返回结果:
        //     A System.Collections.DictionaryEntry containing both the key and the value of
        //     the current dictionary entry.
        //
        // 异常:
        //   T:System.InvalidOperationException:
        //     The System.Collections.IDictionaryEnumerator is positioned before the first entry
        //     of the dictionary or after the last entry.
        DictionaryEntry Entry { get; }
    }
IDictionaryEnumerator继承 IEnumerator,IEnumerator要求实现MoveNext()、Reset()方法。

IEnumerator定义:

    //
    // 摘要:
    //     Supports a simple iteration over a non-generic collection.
    [ComVisible(true)]
    [Guid("496B0ABF-CDEE-11d3-88E8-00902754C43A")]
    public interface IEnumerator
    {
        //
        // 摘要:
        //     Gets the current element in the collection.
        //
        // 返回结果:
        //     The current element in the collection.
        object Current { get; }

        //
        // 摘要:
        //     Advances the enumerator to the next element of the collection.
        //
        // 返回结果:
        //     true if the enumerator was successfully advanced to the next element; false if
        //     the enumerator has passed the end of the collection.
        //
        // 异常:
        //   T:System.InvalidOperationException:
        //     The collection was modified after the enumerator was created.
        bool MoveNext();
        //
        // 摘要:
        //     Sets the enumerator to its initial position, which is before the first element
        //     in the collection.
        //
        // 异常:
        //   T:System.InvalidOperationException:
        //     The collection was modified after the enumerator was created.
        void Reset();
    }
IDictionaryEnumerator 属性DictionaryEntry定义
    //
    // 摘要:
    //     Defines a dictionary key/value pair that can be set or retrieved.
    [ComVisible(true)]
    public struct DictionaryEntry
    {
        //
        // 摘要:
        //     Initializes an instance of the System.Collections.DictionaryEntry type with the
        //     specified key and value.
        //
        // 参数:
        //   key:
        //     The object defined in each key/value pair.
        //
        //   value:
        //     The definition associated with key.
        //
        // 异常:
        //   T:System.ArgumentNullException:
        //     key is null and the .NET Framework version is 1.0 or 1.1.
        public DictionaryEntry(object key, object value);

        //
        // 摘要:
        //     Gets or sets the key in the key/value pair.
        //
        // 返回结果:
        //     The key in the key/value pair.
        public object Key { get; set; }
        //
        // 摘要:
        //     Gets or sets the value in the key/value pair.
        //
        // 返回结果:
        //     The value in the key/value pair.
        public object Value { get; set; }
    }

顺序:System.Web.Caching.Cache ==> IEnumerable{ 实现GetEnumerator,返回IEnumerator类型接口==>{object类型Current、bool型MoveNext、void型Reset} }:Current <== System.Web.Caching.Cache.GetEnumerator返回类型IDictionaryEnumerator{ 实现IEnumerator {DictionaryEntry类型 Entry属性、object类型key value}}:DictionaryEntry

DictionaryEntry类型就对应IEnumerator中的Current的类型,所以在对Cache 执行foreach时的item类型必须是DictionaryEntry类型。

ASP.NET Cache 缓存 遍历 笔记_第1张图片

MSDN官网示例:IEnumerable

示例中Current就是对应的要返回的People 类型。

你可能感兴趣的:(c#基础,c#-web)