Namespaces are C# program elements designed to help you organize your programs. They also provide assistance in avoiding name clashes between two sets of code. Implementing Namespaces in your own code is a good habit because it is likely to save you from problems later when you want to reuse some of your code.
// Namespace Declaration
using System;
// The Portal-management Namespace
namespace Portal-management
{
// Program start class
class NamespaceCSS
{
// Main begins program execution.
public static void Main()
{
// Write to console
Console.WriteLine("This is the new Portal-management Namespace.");
}
}
}
It shows how to create a namespace. We declare the new namespace by putting the word namespace in front of Portal-management. Curly braces surround the members inside the Portal-management namespace.
Nested Namespace
// Namespace Declaration
using System;
// The Portal-management Tutorial Namespace
namespace Portal-management
{
namespace tutorial
{
// Program start class
class NamespaceCSS
{
// Main begins program execution.
public static void Main()
{
// Write to console
Console.WriteLine("This is the new Portal-management Tutorial Namespace.");
}
}
}
}
Another Way to create Namespace using "." operator i.e Portal-management.tutorial
// Namespace Declaration
using System;
// The Portal-management Tutorial Namespace
namespace Portal-management.tutorial
{
// Program start class
class NamespaceCSS
{
// Main begins program execution.
public static void Main()
{
// Write to console
Console.WriteLine("This is the new Portal-management Tutorial Namespace.");
}
}
}
It shows another way of writing nested namespaces. It specifies the nested namespace with the dot operator between Portal-management and tutorial.
Calling Namespace Members: NamespaceCall.cs
// Namespace Declaration
using System;
namespace Portal-management
{
// nested namespace
namespace tutorial
{
class myExample1
{
public static void myPrint1()
{
Console.WriteLine("First Example of calling another namespace member.");
}
}
}
// Program start class
class NamespaceCalling
{
// Main begins program execution.
public static void Main()
{
// Write to console
tutorial.myExample1.myPrint1();
tutorial.myExample2.myPrint2();
}
}
}
// same namespace as nested namespace above
namespace Portal-management.tutorial
{
class myExample2
{
public static void myPrint2()
{
Console.WriteLine("Second Example of calling another namespace member.");
}
}
}
Another way to calling namespaces with the help of "using" keyword or with the Alias Directive:
// Namespace Declaration
using System;
using Portal-management.tutorial;
// Program start class
class UsingDirective
{
// Main begins program execution.
public static void Main()
{
// Call namespace member
myExample.myPrint();
}
}
// Portal-management Tutorial Namespace
namespace Portal-management.tutorial
{
class myExample
{
public static void myPrint()
{
Console.WriteLine("Example of using a using directive.");
}
}
}
With the Alias Directive:
// Namespace Declaration
using System;
using csTut = Portal-management.tutorial.myExample; // alias
// Program start class
class AliasDirective
{
// Main begins program execution.
public static void Main()
{
// Call namespace member
csTut.myPrint();
myPrint();
}
// Potentially ambiguous method.
static void myPrint()
{
Console.WriteLine("Not a member of Portal-management.tutorial.myExample.");
}
}
// Portal-management Tutorial Namespace
namespace Portal-management.tutorial
{
class myExample
{
public static void myPrint()
{
Console.WriteLine("This is a member of Portal-management.tutorial.myExample.");
}
}
}
In summary, you know what a namespace is and you can declare your own namespaces. If you don't want to type a fully qualified name, you know how to implement the using directive. When you want to shorten a long namespace declaration, you can use the alias directive. Also, you have been introduced to some of the other namespace members in addition to the class type.
Convert DATETIME TO TIMESPAN IN C#
13 years ago
No comments:
Post a Comment