Windows Phone Developers

Sunday, May 6, 2012

Using Directive in C#, Create an alias in C#

Before you can use the classes in a given namespace in a C# program, you must add a using directive for that namespace to your C# source file. In some cases, you must also add a reference to the DLL that contains the namespace


The using directive has two uses:
· To allow the use of types in a namespace so that you do not have to qualify the use of a type in that namespace:


using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

· To create an alias for a namespace or a type.

using Excel = Microsoft.Office.Interop.Excel

The using keyword is also used to create using statements, which help ensure that IDisposable objects such as files and fonts are handled correctly. See using Statement for more information.
The scope of a using directive is limited to the file in which it appears.
Create a using alias to make it easier to qualify an identifier to a namespace or type.

Big Statements like the following

oXL = new Microsoft.Office.Interop.Excel.application();

can be replaced with

using Excel = Microsoft.Office.Interop.Excel
oXL = new Excel.application();

Create a using directive to use the types in a namespace without having to specify the namespace. A using directive does not give you access to any namespaces that are nested in the namespace you specify.
Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

1 comment: