What is a "Reference"

G

Guest

Hello all,
two quick questions:

One: What is the difference between adding a reference to something in my C#
project so I can use it (for example, adding a reference to
"Microsft.DirectX" when you need to access DirectX functionality in your
program) and the "using ..." directive?

Two: I've only seen (in books) how to add references to C# projects via the
dropdown menus of the VS.Net 2003 IDE...how do I add references to my C# app
if I'm just using the (free downloadable) .NET SDK and a simple text editor?

Thanks in advance, everyone!
 
N

Nicholas Paldino [.NET/C# MVP]

Lionel,

When you add a reference, it makes your assembly aware of the types in
that reference. When you use the "using" statement at the top of a code
file, it allows you to declare type names without using the full type name.
So, you could do something like:

using System.Text;

StringBuilder builder = new StringBuilder();

As opposed to having to do this (without the using statement):

System.Text.StringBuilder builder = new System.Text.StringBuilder();

If you are using a text editor to develop your code, then you have to
specify the references through the compiler, using the /reference option.

Hope this helps.
 
J

Jan Bannister

Hi Lionel,

Yo must be new :p only kidding.

The using directive when used with a namespace is just a way of not
using a classes fully qualified namespace path in code. (i.e. FileInfo
instead of System.IO.FileInfo).

A reference is a just that, a reference to a library which contains
classes you need to use Like System.Data.dll.

A single refrence can contain classes in many namespaces and conversly
a single namespace can span a number of referenced dlls

type csc /? for help on your second problem.

Hope that helps,
Jan
 
D

Daniel O'Connell [C# MVP]

Lionel said:
Hello all,
two quick questions:

One: What is the difference between adding a reference to something in my
C#
project so I can use it (for example, adding a reference to
"Microsft.DirectX" when you need to access DirectX functionality in your
program) and the "using ..." directive?

The using directive only includes namespaces into the current file. That is
it allows you to use the types in those namespaces without specifying the
namespace. It does not deal with assemblys, just namespaces. References to
assemblies are used to bring in libraries that contain namespaces and types.
Two: I've only seen (in books) how to add references to C# projects via
the
dropdown menus of the VS.Net 2003 IDE...how do I add references to my C#
app
if I'm just using the (free downloadable) .NET SDK and a simple text
editor?

The C# compiler(csc.exe) takes a /reference or /r parameter

csc /r:System.Xml.dl file.cs

would reference the System.Xml assembly. There is also a csc.rsp file in
your framework isntall folder
(x:\windows\microsoft.NET\Framework\v<version>\) which contains a set of
paraemters that the compiler always reads(unless told not to), so you do not
have to specify references to any of the dll's listed there.
 
J

Jon Skeet [C# MVP]

Lionel said:
One: What is the difference between adding a reference to something in my C#
project so I can use it (for example, adding a reference to
"Microsft.DirectX" when you need to access DirectX functionality in your
program) and the "using ..." directive?

A reference links against another assembly. A using directive just
means you don't need to explicitly specify the namespace later on.
Assemblies and namespaces are very different, but you *tend* to end up
with classes in assembly Foo.Bar in the Foo.Bar namespace (and
Foo.Bar.Baz etc).
Two: I've only seen (in books) how to add references to C# projects via the
dropdown menus of the VS.Net 2003 IDE...how do I add references to my C# app
if I'm just using the (free downloadable) .NET SDK and a simple text editor?

When you compile, use the /r: option.
 
J

Jan Bannister

Hi Lionel,

Well That was popular! Wasn't it?

Lionel, I'd recommend having a look at Reflector, a tool by Lutz
Roeder. Its a great way of having a look inside an Assembly and it's
hyperlinked too for easy navigation.


Jan
 

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

Top