using / Add a reference then using

  • Thread starter Thread starter AA2e72E
  • Start date Start date
A

AA2e72E

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?
 
AA2e72E said:
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-
 
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.
 
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top