4

I have a question about the using statement for multiple files at once.

I have created an overload for a class that I want to use in my program. To do so in one of my files I have added the following using statement.

using ClassName = CustomClassName;

This works, but only for that particular file.

Is there a way to get this to work for my entire project?

1
  • 6
    This is a using directive not a using statement Commented Jun 4, 2013 at 13:09

4 Answers 4

6

No.

using directives are per file.

You can always create a template that includes it.

Sign up to request clarification or add additional context in comments.

2 Comments

sample edit template for reference: stackoverflow.com/questions/2072687/…
Thank you for the tip, but it is an existing project, and it is used in a lot of files. So a template is not what i am looking for
5

No; C# does not have any such feature.

Comments

2

This is called type aliasing, re-utilizing the using keyword may be confusing, but it is not an import.

The purpose of this statement is to make a certain class accessible via a different name. This is useful in cases when you have different assemblies linked to your project which accidentally have classes with the same name.

If you for instance have A.dll that defines class Foo under the A namespace, and a B.dll assembly that also defines a Foo class under the B namespace, you can use:

using FooA = A.Foo;
using FooB = B.Foo;

to make distinctions between both.

The scope of this is usually the current file, although, if you happen to define multiple namespaces in the same file, you can scope that to the namespace within the file:

using FooA = A.Foo;

namespace N1
{
    // knows about FooA;
    using FooB = B.Foo;
}

namespace N2
{
    // knows about FooA
    // does not know about FooB
}

Practically you can make this aliasing more defined but no broader than the file's scope.

Comments

2

A simple solution for this is to create a template and use it into you project or class. this will give you a way to use the desire usings and directive as you wish.

here is a good sample to create a template .

http://www.rhyous.com/2010/02/17/how-to-modify-the-default-new-class-template-in-visual-studio-2008/

http://visualstudiomagazine.com/articles/2008/09/01/define-your-own-item-templates.aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.