none
Name of namespaces (using _ underscore in names) RRS feed

  • Question

  • Hello.

    When you create a namespace in C# and start the name with number, the VS IDE rename the namespace by putting underscore (_) before the numbers. It's because it's not possible to begin namespace names with numbers.

    But does this underscore still cause conflicts? Or is it safe to use it?

    My problem is that I need to create namespaces based on each year, for example: Accounts._2017. Is it OK to use this name or does someone recommend to use the name Accounts.Y2017 or similar (using letter instead of underscore in the beginning?


    Sigurður H Alfhildars

    Friday, January 11, 2019 10:05 AM

All replies

  • You can name the namespace name anything you want as long as the compiler considers it legal and can compile the program. So of VS is prefixing it with an " _",  then that's something it's going to do. "_" can be used for local variabke name, _sandri81 or _81sandri signifying that it local to the class or method.
    Friday, January 11, 2019 10:31 AM
  • Hello,

    You could take a different direction if possible and willing to by having logic where account operations are under a specific namespace using a class that is driven by year or a variation of what's shown below rather than focusing on the year as the beginning part of the namespace. otherwise you need to create a new namespace every year.

    namespace AccountSystem
    {
        public class Accounts
        {
            public int Year { get; set; }
            public Accounts(int year)
            {
                Year = year;
                Initialize();
            }
            public Accounts()
            {
                Initialize();
            }
    
            private void Initialize()
            {
                // TODO: do what's needed to work a specific year
            }
        }
    }


    Please remember to mark the replies as answers if they help and unmark 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.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, January 11, 2019 12:12 PM
    Moderator
  • I sort of disagree with creating namespaces with a year in them. What is the business rationale for this? Every year you will create brand new types specific to that year? Does that mean you'll then update all your existing code to use the new types? This seems like a lot of work, especially if the types aren't actually changing. I'm struggling to understand the rationale behind this approach.

    As for the underscore, you can do it but most people in .NET tend to avoid underscores. They just don't read well. In cases where you want to start with a number I would recommend you use a prefix instead. For your specific case I'd lean toward something like this (again, if it made sense):

    Accounts2017
    Accounts.Year2017

    If this is an internal namespace then _2017 would probably be fine because nobody else would see it.


    Michael Taylor http://www.michaeltaylorp3.net

    Friday, January 11, 2019 3:28 PM
    Moderator
  • Hi Sandri81,

    Is there any update? do you try the method that CoolDadTx provided, if the issue still exists, please feel free let us know.

    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.

    Wednesday, January 16, 2019 3:28 AM
    Moderator