Namespaces in C#

Overview

Namespaces are the part of the C# code or we can say that a unit in C# code. Namespaces are used widely in C# and .NET language. Namespaces are simply used for controlling classes and their scope. It contains several class that contains methods in it, and these methods works according to use and requirement of the code.

Types of Declaration

Namespaces are be declared using these two methods in C#.NET:

Through 'using' keyword, such as:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

etc.

Creating your own namespace, such as:

namespace ClassLibrary1
namespace Name
etc.

Sample Snippet

using System;

/*

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

*/

 

namespace ClassLibrary1

{

    public class Class1

    {

        public static void Main()

        {

            Console.WriteLine("Hello C-Sharp Corner");

        }

    }

 

Points to Remember

  • Their scope is defined by using DOT(.) operator
  • Used in organizing the large products
  • Defines scope of classes
  • Used in Controlling the classes
Next Recommended Reading Nested Namespaces in C#