Namespaces in C#
Introduction
Namespaces are C# program elements that allow us to create a system to organize our code. One more very important use, it avoids name clashes between two sets of code. Using namespaces in our code is a good habit because it is likely to save us from problems later when we want to reuse some of our code.
In this post we will talk about namespaces:
/* output:-
Namespaces are C# program elements that allow us to create a system to organize our code. One more very important use, it avoids name clashes between two sets of code. Using namespaces in our code is a good habit because it is likely to save us from problems later when we want to reuse some of our code.
In this post we will talk about namespaces:
(i)
delimited by . (dot) operator and
(ii)
by using 'using' directive
Let's
look at example, in console application to print/write line on console we uses 'Console.WriteLine("Hello");'. This is only possible, if you use 'using System'
directive in code. What is 'System' here? That's the namespace. Let's look at
the ways to use namespaces in program.
(i)
Dot Operator
class Program
{
static void
Main(string[] args)
{
System.Console.WriteLine("Using dot operator.");
System.Console.ReadKey();
}
}
/*
output:-
Using dot
operator.
*/
(ii)
Using 'using' directive
using System;
class Program
{
static void
Main(string[] args)
{
Console.WriteLine("Using 'using' directive.");
Console.ReadKey();
}
}
/*
output:-
Using
'using' directive.
*/
Look
at the difference in both programs; you will notice the use of dot operator and
using directive.
In
this post we are going to create own namespaces.
(i)
Using dot operator
Let's
look at the way to use dot operator in C#.
Program1.cs
using
System;
namespace
demo
{
class Program1
{
public static void
myMethod1()
{
Console.WriteLine("Hello demo Namespace using dot operator.");
}
}
}
Program.cs (From below, anyone
may be used)
using
System;
namespace demo
{
class Program
{
static void Main(string[] args)
{
demo.Program1.myMethod1();
Console.ReadKey();
}
}
}
Hello demo Namespace using dot
operator.
*/
using
System;
class Program
{
static void Main(string[]
args)
{
demo.Program1.myMethod1();
Console.ReadKey();
}
}
/*
output:-
Hello
demo Namespace using dot operator.
*/
In
above programs, I have organized the same program in two different C# files. In
the first program, I have used a namespace 'demo' and inside this I have a
class 'Program1' and inside this a method 'myMethod1()'. Now to use 'myMethod1()' method in Main, I have used its fully qualified path like
namespace_name.class_name.myMethod1();
Actually,
this way is not much useful when we need to call myMethod1() many-times. Let's
re-write Program.cs code bit simpler.
(ii)
Using 'using' directive
Program.cs
using
System;
using
demo;
class Program
{
static void Main(string[]
args)
{
Program1.myMethod1();
Console.ReadKey();
}
}
/*
output:-
Hello
demo Namespace using dot operator.
*/
Please
note, in above program I am using just "class_name.myMethod1()" instead of
fully qualified name "namespace_name.class_name.myMethod1()". And for this I am
using a new namespace "using demo;" at the top.
Nested Namespace
Namespaces
can be nested as well. A good way to organize our namespaces is via a
hierarchical system. We put the more general names at the top of the hierarchy
and get more specific as we go down. This hierarchical system can be
represented by nested namespaces. Look at the program, which has nested system.
using
System;
namespace
demo
{
namespace
test
{
namespace
itorian
{
class
Program
{
static
void Main(string[]
args)
{
Console.WriteLine("Welcome to Nested Namespace.");
Console.ReadKey();
}
}
}
}
}
/*
output:-
Welcome
to Nested Namespace.
*/
So,
that's all about the namespaces (custom namespaces) in C#. I hope you like my
effort here.
Comments
Post a Comment