Namespaces

  • Namespaces are a convenient way of organizing source files (and classes).

  • They are not necessary but highly encouraged.

  • Different namespaces are included at the top of the file with using statements.

Usage

  • Let's take a look at a C# file: Constants.cs

    • Contains a single class.

    • Namespace is ConsoleApp.Common

namespace ConsoleApp.Common
{
    public static class Constants
    {
        public const string CONST_COURSE_NAME = "Learning C#";
    }
}
  • Refer to the constant in the above class from the main program like this:

using ConsoleApp.Common;

namespace ConsoleAppMain
{
    internal class Program
    {
        static void Main(string[] args)
        {
            var appName = Constants.CONST_COURSE_NAME;
            Console.WriteLine(appName);
        }
    }
}

Last updated