Search code examples
c#disposecache-dependency

c# cachedependency dispose pattern


I am adding objects of cache dependency to an array of such AggregateCacheDependency. when I finish with the array I dispose it. my question is do I have to dispose just the array containing the objects or do I have to dispose of the objects them self before?


Solution

  • You don need explicitly dispose the objects within AggregateCacheDependency as it hendles disposal of its objects itself. Here is the metohod of AggregateCacheDependency which disposes each object in its array:

    protected override void DependencyDispose()
            {
                CacheDependency[] array = null;
                bool flag = false;
                try
                {
                    Monitor.Enter(this, ref flag);
                    this._disposed = true;
                    if (this._dependencies != null)
                    {
                        array = (CacheDependency[])this._dependencies.ToArray(typeof(CacheDependency));
                        this._dependencies = null;
                    }
                }
                finally
                {
                    if (flag)
                    {
                        Monitor.Exit(this);
                    }
                }
                if (array != null)
                {
                    CacheDependency[] array2 = array;
                    for (int i = 0; i < array2.Length; i++)
                    {
                        CacheDependency cacheDependency = array2[i];
                        cacheDependency.DisposeInternal();
                    }
                }
            }