views:

945

answers:

7

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

using StringFormat=System.Drawing.StringFormat;

What's that all about?

+24  A: 

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)

Sean Nyman
Namespace /or/ type alias, to be precise.
Richard Hein
Is this a generally accepted way to name your own generics ? e.g. using DestinationMap = System.Collections.Generic.Dictionary<string,Destination>;
nos
@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>
Thomas Levesque
+1  A: 

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

Fiur
+9  A: 

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

GoodEnough
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.
Richard Hein
+1  A: 

It's a alias for the namespace

Fossmo
+3  A: 

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();
Zanoni
+5  A: 

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.

Bullines
+1  A: 

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 */
 }
}
Brian Reiter