Creating Namespaces

Creating a namespace is easy. Just use the word namespace followed by the name. The contents of a namespace are enclosed in curly braces. The following example shows how to create a namespace and add a class to it.

namespace SAMS
{
    using System;
    using aFilePerm =
        System.Security.Permissions.FileIOPermissionAccess;

    public class FilePerm
    {
        aFilePerm fileAccess = new aFilePerm();

        public FilePerm()
        {
            fileAccess = aFilePerm.NoAccess;
        }

        public aFilePerm FileAccess
        {
            get
            {
                return fileAccess;
            }
            set
            {
                fileAccess = value;
            }
        }
    }
}

The first line shows that this code is in the SAMS namespace. The next example shows how to access this class from another class.

 using System; using aFilePerm = System.Security.Permissions.FileIOPermissionAccess; ...

Get C# Unleashed now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.