Home

Namespaces

 

Namespace Fundamentals

 

Introduction

A namespace is a section of code that is identified with a specific name. The name could be anything such as somebody's name, the name of the company's department, or a city. To create a namespace, you start with the namespace keyword followed by the name of the section.

The section that is part of a namespace starts with an opening curly bracket "{" and ends with a closing curly bracket "}". Here is an example:

namespace Business
{
}

Between the curly brackets, you can type anything that is part of the namespace. For example, you can create a class inside of a namespace. Here is an example:

namespace Business
{
    class House
    {
    }
}

Accessing Members of a Namespace

After creating the necessary members of a namespace, you can use the period operator to access an item that is part of the namespace. To do this, in the desired location, type the name of the namespace, followed by a period, followed by the desired member of the namespace. An alternative is to import a namespace. To do this, in the top section of the file, after the page language tag, type

<%@ Import Namespace="Namespace" %>

In this formula, replace Namespace with the actual namespace you want to use. Here is an example:

<%@ Page Language="C#" %>
<%@ Import Namespace="Geometry" %>
<html>
<head>

<title>Exercise</title>
</head>
<body>

</body>
</html>

Namespace Nesting

You can create one namespace inside of another namespace. This is referred to as nesting. The namespace inside of another namespace is nested. To create a namespace inside of another, simply type it as you would create another namespace.

To access a member of a nested namespace, type the name of the external namespace, followed by a period, followed by the nested namespace. Continue this until the lastly nested namespace, followed by a period, followed by the object you want to access.

To import a nested namespace, use the following formula:

<%@ Import Namespace="ParentNamespace.ChildNamespace.GrandChildNamespace" %>

Start with the parent namespace, followed by a period and each nested namespace until the last one. Here is an example:

<%@ Page Language="C#" %>
<%@ Import Namespace="Geometry.Quadrilaterals" %>
<html>
<head>

<title>Exercise</title>
</head>
<body>

</body>
</html>

The System Namespace

 

Introduction

To make programming in C# a little easier, many classes ship with it and they are created in various namespaces of the .NET Framework. Each namespace in C# is used to provide a specific set of classes. The most regularly used namespace in the .NET Framework is called System.

Using a Namespace

We saw that, to call an object or a method that is part of a namespace, you must "qualify" that method or object using the period operator. Instead of using this approach, if you already know the name of a namespace that exists or has been created in another file, you can use a special keyword to indicate that you are using a namespace that is defined somewhere. This is done with the using keyword. To do this, on top of the file (preferably), type using followed by the name of the namespace.

With the using keyword, you can include as many external namespaces as necessary.

.NET Support of Data Types

 

Introduction

All of the data types we have used so far are in fact complete classes. This means that they are equipped with methods. These classes are defined in the System namespace. The classes of these data types are defined as:

C# Data Type Equivalent .NET Class C# Data Type Equivalent .NET Class
bool Boolean char Char
byte Byte sbyte SByte
short Int16 ushort UInt16
int Int32 uint UInt32
long Int64 ulong UInt64
  float Single   double Double
  decimal Decimal      

This means that, if you don't want to use the data types we have reviewed so far, you can use the class that is defined in the System namespace. To use one of these types, type the System namespace followed by a period. Then type the equivalent class you want to use.

 
 
 
     
 

Home Copyright © 2009-2016, FunctionX, Inc.