Did you know…? (using syntax in C#)


Most of the C# files I read are structured similarly to the following...

 

/*

 * My fancy header du jour.

 */

 

using System;

using System.Data;

using Other.Stuff;

 

namespace My.Namespace.Is.Better.Than.Yours

{

  // Code goes here.

}

 

Personally, I've adopted the following convention. Note where the using statements were moved to.

 

/*

 * Yes, I have a fancy header, as well.

 */

 

namespace My.Namespace.Is.Better.Than.Yours

{

  using System;

  using System.Data;

  using Other.Stuff;

 

  // Code goes here.
}

 

This limits the scope of the using statements to the enclosing curly braces. Which means that I can get my 10 file project, run a copy command from a console, and get a neat, single-file project, that ends up looking like this:

 

/* The first file */

namespace Whatever

{

  using Foo;

  // Code

}


 

/* The second file */

namespace BlahBlahBlah

{

  using Something.That.Might.Otherwise.Conflict;

  // Code

}

...

 

And, for those of you who don't quite remember how the syntax was for the copy command in "append stuff mode", here y'go.

 

copy file1.cs + file2.cs + file3.cs MyReallyLargeFile.cs

 

 

This posting is provided "AS IS" with no warranties, and confers no rights.

 


Comments (3)

  1. I use the same approach, although not for the same reasons.

    Resharper can automatically arrange ALL your usings into this style with one keystroke, too.

  2. Joku says:

    While this may not be directly applied to, I’d like to see more functionality on the virtual folders in the Solution Explorer. I mean suppose I have multiple files that I do not want to be compiled, but I still like them visible on the VS view. Of course I tried to put them under a new folder and look at the folder properties for the same properties as the files have, allowing to override the default Compile behaviour for all files in the folder with a single click, just like you do in the real Explorer when overriding the Read Only attribute for example. As long as the .cs files are positioned under that folder they should inherit the folders properties or something right?

    Anyhow it could be also made so that if you put these files under such folder, there would be a folder property that when true does something like you are doing by hand here.. Maybe..

  3. And you find this result useful because?…

    Sorry, but I’m failing to see the usefulness of having everything in honking great big file.

Skip to main content