Namespace in C#

Introduction

Namespace in C# development is very heavily used regularly; it is helpful when we develop a relatively large or complex project.  For instance, .NET uses namespace to organise its classes.

When it comes to using namespace in C#, it allows us to access the class of namespace by using namespace.classname syntax.  Alternatively, we can use keyword using syntax, so we don’t need to use a complete name to access a class.


Access the class in a different namespace – Fully Qualified Name

In the above code, we declare a new namespace called Greeting; in the Greeting namespace, we define a class called MorningGreet; within the class, there is a GoodMorning Method.

In Line 17, we need to disclose the spacename Greeting when we make an instance morning of MorningGreet class. Otherwise, the compiler can’t find where is the class from, even though the class is declared as public.


Another Approach – Keyword Using

We did some modification on the code snippet by add namespace Greeting on line 3.

As a result, when we need to access MorningGreet class, we don’t need to use the fully qualified name of the class. This is quite useful when we develop a relatively large or complex project.


Leave a Reply

Your email address will not be published. Required fields are marked *