Hiding inherited members

前端 未结 9 1915
北海茫月
北海茫月 2020-12-03 09:52

I\'m looking for some way to effectively hide inherited members. I have a library of classes which inherit from common base classes. Some of the more recent descendant clas

9条回答
  •  一整个雨季
    2020-12-03 10:19

    You can use an interface

        public static void Main()
        {
            NoRemoveList testList = ListFactory.NewList();
    
            testList.Add(" this is ok ");
    
            // not ok
            //testList.RemoveAt(0);
        }
    
        public interface NoRemoveList
        {
            T this[int index] { get; }
            int Count { get; }
            void Add(T item);
        }
    
        public class ListFactory
        {
            private class HiddenList: List, NoRemoveList
            {
                // no access outside
            }
    
            public static NoRemoveList NewList()
            {
                return new HiddenList();
            }
        }
    

提交回复
热议问题