代码如下:

难点重写索引器、重写基类方法、基类方法显示调用示例

generic <class T> public ref class SyncList : public List<T>
    {
    private:
        Object^ _rootLock = gcnew Object();

    public:
        virtual property T default[int]
        {
            T get(int index) new
            {
                try
                {
                    Monitor::Enter(_rootLock);
                    return List::default[index];
                }
                finally
                {
                    Monitor::Exit(_rootLock);
                }
             }
             void set(int index,T value) new
             {
                 try
                 {
                    Monitor::Enter(_rootLock);
                    List::default[index] = value;
                 }
                 finally
                 {
                    Monitor::Exit(_rootLock);
                 }
             }
        }

    public:
        virtual void Add(T item)new
        {
            try
            {
                Monitor::Enter(_rootLock);
                List::Add(item);
            }
            finally
            {
               Monitor::Exit(_rootLock);
            }
        }

        virtual void AddRange(IEnumerable<T>^ items)new
        {
            try
            {
                Monitor::Enter(_rootLock);
                List::AddRange(items);
            }
            finally
            {
               Monitor::Exit(_rootLock);
            }
        }

        virtual void Remove(T item)new
        {
            try
            {
                Monitor::Enter(_rootLock);
                List::Remove(item);
            }
            finally
            {
               Monitor::Exit(_rootLock);
            }
        }

        virtual void RemoveAt(int index)new
        {
            try
            {
                Monitor::Enter(_rootLock);
                List::RemoveAt(index);
            }
            finally
            {
               Monitor::Exit(_rootLock);
            }
        }
    };

 

相关文章:

  • 2021-12-04
  • 2022-12-23
  • 2022-01-10
  • 2021-05-29
猜你喜欢
  • 2022-12-23
  • 2022-01-10
  • 2022-12-23
  • 2021-09-11
  • 2022-02-18
  • 2021-11-15
相关资源
相似解决方案