示例#1
0
        public static void ShowDestructor()
        {
            ResourceHolderDerived resourceHolderDerived = new ResourceHolderDerived();
            ResourceHolderBase    resourceHolderBase    = new ResourceHolderBase();

            resourceHolderBase    = null;
            resourceHolderDerived = null;

            Console.WriteLine("\nShowing destructors work: ");
            GC.Collect(0, GCCollectionMode.Forced, true);
        }
示例#2
0
        public static void ShowDispose()
        {
            ResourceHolderDerived resourceHolderDerived = new ResourceHolderDerived();
            ResourceHolderBase    resourceHolderBase    = new ResourceHolderBase();

            Console.WriteLine("Disposing derived:");
            resourceHolderDerived.Dispose();

            Console.WriteLine("\nDisposing base:");
            resourceHolderBase.Dispose();

            Console.WriteLine("\nDisposing derived one more time:");
            resourceHolderDerived.Dispose();
            Console.WriteLine("->No exception here\n");
        }
示例#3
0
        static void Main(string[] args)
        {
            var baseHolder = new ResourceHolderBase();

            using (baseHolder)
            {
                baseHolder.BaseAction();
            }
            using (var derivedHolder = new ResourceHolderBase())
            {
                derivedHolder.BaseAction();
            }
            baseHolder.Dispose();

            Console.ReadLine();
        }