#521 – Namespaces Help Organize Types

In C#, a namespace is a collection of related types.  The fully qualified name of every type actually includes the namespace that it’s defined in.

You use the namespace keyword to define a namespace.  All types defined within the pair of braces that follow the namespace name then belong to that namespace.

In the example below, the Dog class belongs to the DogLibrary namespace.

namespace DogLibrary
{
    public class Dog
    {
        public string Name { get; set; }
        public int Age { get; set; }

        public void Bark()
        {
            Console.WriteLine("WOOOOOF!");
        }
    }
}

Because Dog is defined within the DogLibrary namespace, the fully qualifed name of the Dog type is DogLibrary.Dog.

Advertisement

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

3 Responses to #521 – Namespaces Help Organize Types

  1. Pingback: #522 – The Fully Qualified Name for a Type Includes the Namespace « 2,000 Things You Should Know About C#

  2. Pingback: #523 – The using Directive Allows Omitting Namespace « 2,000 Things You Should Know About C#

  3. Pingback: #530 – Namespaces vs. Assemblies « 2,000 Things You Should Know About C#

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: