Go out of using scope in C#

Shervan360 1,481 Reputation points
2022-09-27T18:06:04.14+00:00

Hello,

I'd like to go out of "using" scope. How can I do this?

using System;  
  
internal class Program : IDisposable  
{  
    static void Main(string[] args)  
    {  
  
        using Program p = new();  
        Console.WriteLine("Hello World!");  
        int a = 10;  
        {  
            // I'd like to go out of "using" scope. How can I do this?  
            Console.WriteLine(a);  
        }  
    }  
  
    public void Dispose()  
    {  
        Console.WriteLine("I'm here...");  
    }  
}  
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,650 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Tommy 6 Reputation points
    2022-09-28T07:33:53.89+00:00

    The using scope ends at the end of the method.
    The c# 8 metod:

      internal class Program  
      {  
          static void Main(string[] args)  
          {  
              using Program p = new();            
              Console.WriteLine("Hello World!");  
          } // <-- using scope ends here  
      }  
    

    Is just a shortcut for writing:

      internal class Program  
      {  
          static void Main(string[] args)  
          {  
              using (Program p = new())  
              {  
                  Console.WriteLine("Hello World!");  
              } // <-- using scope ends here  
          }  
      }  
    

    If you want to get out of the using scope you will need to use braces in some way.
    You could use the braces like this instead, but I'm not sure what the advantage of that would be.

      internal class Program  
      {  
          static void Main(string[] args)  
          {  
              {  
                  using Program p = new();  
                  Console.WriteLine("Hello World!");  
              } // <-- using scope ends here  
              Console.WriteLine("Out of scope");  
          }  
      }  
    

    Or extract it to a new method

      internal class Program  
      {  
          static void Main(string[] args)  
          {  
              ExtractedToMethod();  
              Console.WriteLine("Out of scope");  
          }  
            
          static void ExtractedToMethod()  
          {  
              using Program p = new();  
              Console.WriteLine("I'm in scope");  
          } // <-- using scope ends here  
      }  
    
    1 person found this answer helpful.

  2. Tommy 6 Reputation points
    2022-09-27T22:18:55.823+00:00

    To end the "using" scope earlier, you need to somehow make "Program p" go out of scope.
    Different ways to do that illustrated here: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-statement

     using System;  
          
     internal class Program : IDisposable  
     {  
         static void Main(string[] args)  
         {  
          
             using (Program p = new())  
             {  
                 Console.WriteLine("Hello World!");  
             } // <-- "p" goes out of scope and is disposed at this point.  
             int a = 10;  
             {  
                 // I'd like to go out of "using" scope. How can I do this?  
                 Console.WriteLine(a);  
             }  
         }  
          
         public void Dispose()  
         {  
             Console.WriteLine("I'm here...");  
         }  
     }  
    

  3. Jack J Jun 24,496 Reputation points Microsoft Vendor
    2022-09-28T05:33:38.803+00:00

    @Shervan360 , you could call Dispose Method to replace the using scope, like the following code:

    When you use the using statement, it means that the class will call the Dispose Method automatically.

      internal class Program : IDisposable  
        {  
            static void Main(string[] args)  
            {  
                Program p = new();  
                try  
                {  
                    Console.WriteLine("Hello World!");  
                    int a = 10;  
                    Console.WriteLine(a);  
                }  
                catch  
                {  
      
                }  
                finally  
                {  
                    p.Dispose();  
                }  
                 
            }  
      
            public void Dispose()  
            {  
                Console.WriteLine("I'm here...");  
            }  
        }  
    

    The answer comes from stack overflow also describes it vey clearly and you could have a look.

    Hope my answer could help you.

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments