示例#1
0
        public void CallRiskyWithinCallRiskys_ThisJustGotUgly()
        {
            //Sometimes people found they needed the result of one risky operation to create an object for another risky operation
            var             disposable = new RiskyDisposable();
            RiskyDisposable soonToBeCreatedDisposable = null;

            try
            {
                var result = disposable.RiskyMethod();
                soonToBeCreatedDisposable = new RiskyDisposable(result);
                soonToBeCreatedDisposable.RiskyMethod();
            }
            catch
            {
            }
            finally
            {
                disposable.Dispose();
                //thanks to null propogation we no longer need the if statement asking if it's null lol
                soonToBeCreatedDisposable?.Dispose();
            }

            Assert.True(disposable.IsDisposed);
            if (soonToBeCreatedDisposable != null)
            {
                Assert.True(soonToBeCreatedDisposable.IsDisposed);
            }
        }
示例#2
0
        public void AlsoUsingsLookPrettierWhenDoingNonRiskyThings()
        {
            using (var disposable = new RiskyDisposable())
                disposable.NonRiskyMethod();

            //vs

            var disposable2 = new RiskyDisposable();

            disposable2.NonRiskyMethod();
            disposable2.Dispose();
        }
示例#3
0
        public void CallRiskyDisposableWithoutProtection_NotDisposed()
        {
            //Before if an exception was thrown things might not be cleaned up if you were not being careful
            var disposable = new RiskyDisposable();

            try
            {
                disposable.RiskyMethod();
                disposable.Dispose();
            }
            catch
            {
            }

            Assert.False(disposable.IsDisposed);
        }
示例#4
0
        public void CallRiskyDisposableTheOldSafeWay_Disposed()
        {
            //People solved a way to do it safely though you just had to write a few lines
            var disposable = new RiskyDisposable();

            try
            {
                disposable.RiskyMethod();
            }
            catch
            {
            }
            finally
            {
                disposable.Dispose();
            }

            Assert.True(disposable.IsDisposed);
        }