locked
Can we remove the brackets after namespace? RRS feed

  • Question

  • Just like the using statement, we can omit the { } now

        using var test = new something();

    The brackets after namespace is utterly unnecessary.

    Thursday, April 30, 2020 12:28 PM

All replies

  • No the brackets are required.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, April 30, 2020 12:38 PM
  • This was introduced in 2019.

    The brackets are implied by the scope of the using variable.

    e.g., 

    using var test = new something();

    Foo();

    is equivalent to:

    using (var test = new something())
    {
        Foo();
    }

    This is yet another one of those C# language "improvements" designed mainly to keep language designers out of trouble.  I can't imagine anyone asking for this.


    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter


    Thursday, April 30, 2020 1:04 PM
  • What are you talking about? Are you talking about when declaring a namespace?

    namespace Test
    {
    }

    How is this useless? It identifies the namespace of the contained elements. If it weren't for that then this wouldn't work.

    namespace Test
    {
       namespace Tech
       {
           class InTechNamespace { }
       }
    
       class InTestNamespace { }
    }
    This is how C-based languages work. Even VB follows a similar format.


    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, April 30, 2020 1:12 PM
  • What are you talking about? Are you talking about when declaring a namespace?

    namespace Test
    {
    }

    How is this useless? It identifies the namespace of the contained elements. If it weren't for that then this wouldn't work.

    namespace Test
    {
       namespace Tech
       {
           class InTechNamespace { }
       }
    
       class InTestNamespace { }
    }
    This is how C-based languages work. Even VB follows a similar format.


    Michael Taylor http://www.michaeltaylorp3.net

    We are not talking about declaring namespaces.  We are talking about a new C# feature introduced last year to imply the scope of a using statement.

    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

    Thursday, April 30, 2020 1:18 PM
  • "The brackets after namespace is utterly unnecessary."

    I know what the using statement is. But why would the OP ask about brackets after a namespace on a using statement? Namespaces have nothing to do with the using statement. They do have something to do with the using directive and with the namespace statement. I'm waiting for the OP to clarify their question.


    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, April 30, 2020 1:32 PM
  • Yes, the choice of words was curious.  But the example was about the new feature (but missing parentheses).  Clarification is in order.

    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

    Thursday, April 30, 2020 1:37 PM
  • And it will just get worse, since C# is basically being designed on GitHub now...

    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

    Thursday, April 30, 2020 1:39 PM
  • On the plus side, they are going 'soooo' overboard with trying to do more with less instructions that soon we will have obfuscation for free :-(

    Andy

    Thursday, April 30, 2020 2:23 PM
  • On the plus side, they are going 'soooo' overboard with trying to do more with less instructions that soon we will have obfuscation for free :-(

    Andy

    This is what happens when language designers get bored. They should move on to a new language. C# is just a step away from being as monstrous as C++.

    I can't believe that someone actually said "it's a real pain to type in 2 curly braces every time I want a 'using' statement".


    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

    Thursday, April 30, 2020 3:49 PM
  • Hi CKKwan,

    Thank you for posting here.

    According to your description, you would like to remove the brackets when we use 'using' statement.

    You can try to use try..finally and dispose method to make it.

    I make a code example and you can refer to it.

    When we use using with the brackets:

    string connectionstring = "";
                using (SqlConnection conn = new SqlConnection(connectionstring))
                {
                    // Some Code
                }

    When we use using without the brackets:

                string connectionstring = "";
                SqlConnection conn = null;
                try
                {
                     conn = new SqlConnection(connectionstring);
                    // some code
                }
                finally
                {
                    conn.Dispose();
                }

    Hope my code can help you.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, May 1, 2020 1:54 AM
  • Dave wrote:

    >    using (var test = new something());
    >    Foo();
    > is equivalent to:
    >    using (var test = new something())
    >    {
    >        Foo();
    >    }

    Now, I am not a C# language lawyer, but I'd bet real dollars that the semicolon at the end of your first statement is a mistake.  Otherwise, that would be an incredible opportunity for errors.

    -- 

    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.


    Friday, May 1, 2020 6:53 PM
  • Dave wrote:

    >    using (var test = new something());
    >    Foo();
    > is equivalent to:
    >    using (var test = new something())
    >    {
    >        Foo();
    >    }

    Now, I am not a C# language lawyer, but I'd bet real dollars that the semicolon at the end of your first statement is a mistake.  Otherwise, that would be an incredible opportunity for errors.

    -- 

    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.


    No - unfortunately that part is correct - the scope of the 'using declaration' is the local scope of the variable being declared.  What was incorrect was the parentheses I included in the newer syntax version - I edited my original answer to correct that.

    This 2019 syntax feature is called "using declaration".

    i.e.,

    using var test = new something();
    Microsoft should take back control over the C# language - every idiot's half-baked suggestion on GitHub is now becoming part of the C# language.


    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter




    Friday, May 1, 2020 6:56 PM
  • Hello,

     Sorry, if my reply is late. But clearly there is some

    confusion about  OP request, clarity important;

     Square brackets [] = object array, index holder

     Curly brackets {} = object scope, namespace, class, etc.

     Parentheses () = objects parameters, method syntax, etc.

     

     Remember IDE is a Script editor and as such the parser

    need to know what is what.

     

     Hope this helps :/

    Sunday, May 3, 2020 4:02 AM
  • Even though most people here don't see any benefit in omitted curly brackets, I believe that it's rather handy when you have multiple disposable objects in a single scope (like a single method).

    Previously, each of those objects would indent the block of code and that may end up being unnatural to read, it's easier on the eyes to have it like this.
    For anyone that's interested I highly recommend watching Kevlin Henney's presentation about this topic.

    I know, you could avoid this by explicitly calling Dispose methods yourself, but then you would need to do it in finally block to get the exact equivalent and that just adds more noise to your code...
    Anyway, my point is that too many vertical lines of attention (indentations) in a relatively small code block can seem visually very busy.
    Of course, it all depends on the context, but nevertheless the point remains for most cases.

    However, in case of the namespaces that doesn't apply. As CoolDadTx showed the problem is that namespaces themselves are the top scopes. Without their brackets, you wouldn't be able to have nested namespaces nor multiple namespaces in the single file.

    In other words, what would be the benefit of having this, what problem would it solve? I truly don't see any reading issue with namespace(s).

    Saturday, May 9, 2020 4:37 AM
  • hertzogth, you can nest usings without extra curly brackets:

    using (MyStuff stuff = new MyStuff())
    using (MyOtherStuff other = new MyOtherStuff())
    using (SqlConnection conn = new SqlConnection())
    {
        // all your code here
    }



    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Sunday, May 10, 2020 2:12 PM
  • On the plus side, they are going 'soooo' overboard with trying to do more with less instructions that soon we will have obfuscation for free :-(

    Andy

    This is what happens when language designers get bored. They should move on to a new language. C# is just a step away from being as monstrous as C++.

    I can't believe that someone actually said "it's a real pain to type in 2 curly braces every time I want a 'using' statement".


    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

    Agree in regards to typing two brackets while at the same time the majority of brackets can be done for us via tab-tab e.g. type if tab-tab etc. Then there is Resharper which makes it even easier.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, May 10, 2020 3:54 PM