CS.IDISP.USING

Using statement should be used for constructing objects that implement IDisposable.

Use a Using statement to create an object that implements the IDisposable interface.

Vulnerability and risk

Objects that implement the IDisposable interface must call the Dispose () method to release resources. Forgetting to call the Dispose () method can cause a resource leak problem.

Mitigation and prevention

By using a Using statement, the Dispose () method will be called automatically, so you can avoid missing calls to the Dispose () method.

Example

Copy
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  
  namespace kmcustom
  {
  
     class MyCls
     {
         public string _str = "abc";
 
         public void setStr(string str)
         {
             _str = str;
         }
         public string getStr()
         {
             return _str;
         }
 
 
     }
 
     class C16
     {
         private string str = "abc";
 
         private String cstr = "abc";
 
         private int number = 1;
 
         private int[] int_array = new int[5];
 
         private MyCls myCls = new MyCls();
 
         public string getStr()
         {
             return str;//OK - string or String is immutable
         }
 
         public string getCStr()
         {
             return str;//OK - string or String is immutable
         }
 
         public int getNumber()
         {
             return number;//OK
         }
 
         public MyCls getMyCls()
         {
             return myCls;//NG
         }
 
         public int[] getIntArray()
         {
             for(int i = 0; i < int_array.Length; i++) 
             {
                 int_array[i] = 0;
             }
             return int_array;//NG
         }
 
         public void printIntArray()
         {
             for (int i = 0; i < int_array.Length; i++) {
                 Console.WriteLine("index:" + i + " value:" + int_array[i]);
             }
 
         }
     }
 }