TICS Coding Standard Viewer 
TIOBE Software Quality Framework
Print-friendly version
©TIOBE Software www.tiobe.com
 
C# Coding Standard
Search

Rule:  5@121Checked automatically with code checker

Synopsis:Don't use "using" variables outside the scope of the "using" statement
Language:C#
Severity Level:1
Category:Object lifecycle


Description:

The "using" statement is a convenient way to dispose objects. While using this C# feature, one should make sure that the disposed object is not used outside the scope of the "using" statement.

For example:

        private SsisPackageTester CreateSsisPackageTester(string workbookFile)
        {
            using (SsisPackageTester tester = CreateSsisPackageTester(workbookFile))
            {
                tester.SetAssertionVisitors(DefaultAssertionVisitors);
                return tester;
            }
        }

In this example "tester" is referring to a non-existing object after the "return" statement. Returning a disposed object in this way is an unintended programming error.