471,330 Members | 1,041 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,330 software developers and data experts.

using / Add a reference then using

I am trying to understand what is happening when

1. I simply write

using System.Globalisation

2. I add a reference to a dll then write

using xxxxxx

Can someone put the explanation in some words, please?
Jun 27 '08 #1
4 1096
AA2e72E wrote:
I am trying to understand what is happening when

1. I simply write

using System.Globalisation

2. I add a reference to a dll then write

using xxxxxx

Can someone put the explanation in some words, please?

First you add the DLL (assembly in .NET jargon). This is essential.
This makes the contents of the assembly available to you code and it is
also linked in to Visual Studio's Intellisense etc.

Second, you can optionally put a 'using statement' in your code (top of
file or top of namespace). A 'using statement' only activates a shortcut
mechanism, it has no 'real' semantics.

Example:

using System;
... Console.WriteLine(); // Console is abbreviated

or

... System.Console.WriteLine(); // System.Console is the Full name

We normally use a 'using' but sometimes it's more convenient and/or more
clear to use the long (full) names. For example when another namespace
also has a Console class.
-HH-


Jun 27 '08 #2
On Fri, 23 May 2008 10:00:08 -0700, AA2e72E
<AA*****@discussions.microsoft.comwrote:
I am trying to understand what is happening when

1. I simply write

using System.Globalisation

2. I add a reference to a dll then write

using xxxxxx

Can someone put the explanation in some words, please?
In either case, "using" does the same thing. It allows members of the
given namespace to be used without qualification.

For every class not implemented in your own assembly, you also need to add
a reference to the assembly where that class is implemented. The members
of the System.Globalization namespace (AFAIK, you're not allowed to spell
it the British way) are, AFAIK, all implemented in the core "mscorlib.dll"
assembly, which your project will have already referenced by default in
most cases, so you can just add the namespace directive without adding a
new reference to the project.

But for some other DLL, especially one not already known to the IDE as
required for the specific project template you started with, you have to
add the reference explicitly.

Pete
Jun 27 '08 #3
On May 23, 1:00*pm, AA2e72E <AA2e...@discussions.microsoft.comwrote:
I am trying to understand what is happening when

1. I simply write

using System.Globalisation

2. I add a reference to a dll then write

using xxxxxx

Can someone put the explanation in some words, please?
Hi,

It's the same, you can create a namespace like that if you want.
if you name your dll MyCompany.MyNamespace you will end with the same
thing.
Jun 27 '08 #4
On May 23, 2:02 pm, "Ignacio Machin ( .NET/ C# MVP )"
<ignacio.mac...@gmail.comwrote:
On May 23, 1:00 pm, AA2e72E <AA2e...@discussions.microsoft.comwrote:
I am trying to understand what is happening when
1. I simply write
using System.Globalisation
2. I add a reference to a dll then write
using xxxxxx
Can someone put the explanation in some words, please?

Hi,

It's the same, you can create a namespace like that if you want.
if you name your dll MyCompany.MyNamespace you will end with the same
thing.
Unfortunately, C# overloads the word "using" so that it has two quite
different meanings.

Writing "using System.Globalisation;" in the top of a file means that
you can abbreviate the names of classes in that namespace without
always having to prefix "System.Globalisation" to the front of the
class name. The license to abbreviate names (i.e., drop the
namespace) lasts throughout the file.

Writing the following is very different:

using System.Globalisation.Classname
{
// some code here uses the Classname object
}

The above code creates an object of type Classname for you; it lets
you use that object between the curly braces, and the runtime then
automatically "disposes" the object for you after control passes below
the braces.
Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

28 posts views Thread by Daniel | last post: by
7 posts views Thread by sbobrows | last post: by
2 posts views Thread by Craig Nicol | last post: by
4 posts views Thread by Elad Gutman | last post: by
29 posts views Thread by shuisheng | last post: by
68 posts views Thread by Jim Langston | last post: by
65 posts views Thread by Arjen | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.