Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • C# - Get Started
  • C# - Version History
  • C# - First Program
  • C# - Keywords
  • C# - Class and Objects
  • C# - Namespace
  • C# - Variables
  • C# - Implicitly-Typed Variables
  • C# - Data Types
  • Numbers
  • Strings
  • DateTime
  • Structure
  • Enum
  • StringBuilder
  • Anonymous Types
  • Dynamic Types
  • Nullable Types
  • C# - Value & Reference Types
  • C# - Interface
  • C# - Operators
  • C# - if else Statements
  • C# - Ternary Operator ?:
  • C# - Switch
  • C# - For Loop
  • C# - While Loop
  • C# - Do-while Loop
  • C# - Partial Class
  • C# - Static
  • C# - Array
  • Multidimensional Array
  • Jagged Array
  • C# - Indexer
  • C# - Generics
  • Generic Constraints
  • C# - Collections
  • ArrayList
  • List
  • SortedList
  • Dictionary
  • Hashtable
  • Stack
  • Queue
  • C# - Tuple
  • C# - ValueTuple
  • C# - Built-in Exceptions
  • Exception Handling
  • throw
  • Custom Exception
  • C# - Delegates
  • Func Delegate
  • Action Delegate
  • Predicate Delegate
  • Anonymous Methods
  • C# - Events
  • C# - Covariance
  • C# - Extension Method
  • C# - Stream I/O
  • C# - File
  • C# - FileInfo
  • C# - Object Initializer
  • OOP - Overview
  • Object-Oriented Programming
  • Abstraction
  • Encapsulation
  • Association & Composition
  • Inheritance
  • Polymorphism
  • Method Overriding
  • Method Hiding
  • C# - Solid Principles
  • Single Responsibility Principle
  • Open/Closed Principle
  • Liskov Substitution Principle
  • Interface Segregation Principle
  • Dependency Inversion Principle
  • Design Patterns
  • Singleton
  • Abstract Factory
  • Factory Method
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

C# Namespace

Namespaces play an important role in managing related classes in C#. The .NET Framework uses namespaces to organize its built-in classes. For example, there are some built-in namespaces in .NET such as System, System.Linq, System.Web, etc. Each namespace contains related classes.

A namespace is a container for classes and namespaces. The namespace also gives unique names to its classes thereby you can have the same class name in different namespaces.

In C#, a namespace can be defined using the namespace keyword.

Example: Namespace
namespace School
{
    // define classes here

}

The following namespace contains the Student and Course classes.

Example: Namespace
namespace School
{
    class Student
    {

    }

    class Course
    {

    }
}

Classes under the same namespace can be referred to as namespace.classname syntax. For example, the Student class can be accessed as School.Student.

Example: Refer a Class with Namespace
namespace CSharpTutorials
{
    class Program
    {
        static void Main(string[] args)
        {
            School.Student std = new School.Student();
        
            School.Course cs = new School.Course();
        }
    }
}

To use classes under a namespace without the fully qualified name, import the namespace with the using keyword at the top of C# class file.

Example: Namespace
using System; //built-in namespace
using School;

namespace CSharpTutorials
{
    class Program
    {
        static void Main(string[] args)
        {
            Student std = new Student();
        }
    }
}

A namespace can contain other namespaces. Inner namespaces can be separated using (.).

Example: Namespace
namespace School.Education
{
    class Student
    {

    }
}

In the above example, the fully qualified class name is School.Education.Student.

Beginning with C# 10, you can declare a namespace for all types defined in that file without wrapping classes inside curly braces { }, as shown below.

Example: C# 10 Namespace
namespace School.Education
    
class Student
{

}
TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.