What's this C# "using" directive?
Asked Answered
C

7

16

I saw this C# using statement in a code example:

using StringFormat=System.Drawing.StringFormat;

What's that all about?

Colis answered 23/6, 2009 at 20:44 Comment(2)
To aid in the prevention of namespace confusionInflated
In this example it "imports" the StringFormat class name without the rest of the members of the System.Drawing namespace.Mathia
R
33

That's aliasing a typename to a shorter name. The same syntax can also be used for aliasing namespaces. See using directive.

(Updated in response to Richard)

Rigorous answered 23/6, 2009 at 20:46 Comment(3)
Namespace /or/ type alias, to be precise.Towroy
Is this a generally accepted way to name your own generics ? e.g. using DestinationMap = System.Collections.Generic.Dictionary<string,Destination>;Sheepfold
@noselasd : no, it's not common practice. Of course, you can do it if you want, but I would strongly advise against it, because it's very confusing... It would be better to create a DestinationMap class that inherits from Dictionary<string, Destination>Attic
P
13

It's an alias, from now on, the user can use StringFormat to refer to System.Drawing.StringFormat. It's useful if you don't want to use the whole namespace (in case of name clash issues for example).

source: using Directive article from MSDN

Philosophical answered 23/6, 2009 at 20:46 Comment(1)
I was going to say it doesn't make sense to use the same name, but since you mentioned that it will avoid including the entire namespace, it does make sense. Without this point, the rest of the answers are incomplete.Towroy
P
7

Perhaps a different, unrelated StringFormat is declared in another namespace like Acme.Stuff. If that were the case, this would cause confusion:

using System.Drawing; // Contains StringFormat type.
using Acme.Stuff;  // Contains another StringFormat type.

private void Foo()
{
    StringFormat myFormat = new StringFormat(); // which one to use?
}

Aliasing is with using on the StringFormat=System.Drawing.StringFormat clears up some of the confusion.

Philippopolis answered 23/6, 2009 at 20:48 Comment(0)
B
3

It means you're using StringFormat as an alias for System.Drawing.StringFormat;

Brachy answered 23/6, 2009 at 20:46 Comment(0)
S
3

This will define an alias to System.Drawing.StringFormat.

That's the same thing like this example:

using SQL = System.Data.SqlServer;

SQL.SqlConnection sql = new SQL.SqlConnection();
Stele answered 23/6, 2009 at 20:47 Comment(0)
B
2

It's a alias for the namespace

Bummalo answered 23/6, 2009 at 20:47 Comment(0)
M
1

The using keyword is used for importing namespaces or aliasing classes or for managing scope on disposable objects. Here we are talking about the namespace usage.

using StringFormat=System.Drawing.StringFormat;

The way using was used here is a little unusual in C# but more common in Java import statements. What it does is provide a StringFormat alias without importing the entire System.Drawing namespace. Some people with a Java background like to proactvely import only the classes being used rather than whole anmespaces (aka Java packages). Arguably you proactively avoid potential name conflicts if you import only specific class names but it isn't very common in C# and Visual Studio doesn't encourage it the way, say, Netbeans does for Java.

The more common usuage of aliasing is to resolve class names to a shortened alias when there is a naming conflict.

using System.Drawing;
using AwesomeCompany.ReallyAwesomeStuff.AwesomeLibrary.Drawing;
/* AwesomeCompany.ReallyAwesomeStuff.AwesomeLibrary.Drawing has a StringFormat class */
using AwesomeStringFormat = AwesomeCompany.ReallyAwesomeStuff.AwesomeLibrary.Drawing.Stringformat;
using StringFormat = System.Drawing.StringFormat;

public class AwesomeForm() : Form
{
    private AwesomeForm()
    {
        AwesomeStringFormat stringFormat = new AwesomeStringFormat();
        stringFormat.Color = Color.Red;
        /* etc */
    }
}
Mathia answered 23/6, 2009 at 21:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.