Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access 'using' directive from different files

Tags:

c#

Consider the next solution structure:

File 1:

using MyClass = System.Collections.Generic.List<int>;

namespace NamespaceA
{
    class  A
    {
        MyClass a;
    }
}

namespace NamespaceB
{
    class B
    {
        MyClass b;
    }
}

File 2:

namespace NamespaceC
{
    class C
    {
        MyClass c; // <-- The type or namespace name 'MyClass' could not be found 
    }
}

How can I access to MyClass definition from File2? I tried to move it into NamespaceA and use using MyClass = NamespaceA.MyClass in File2, but had no success. My goal is to define complicate type in one place, not in all files where it is used.

like image 406
Miamy Avatar asked Jan 20 '26 15:01

Miamy


1 Answers

This is called a "using alias directive."

Per MSDN:

The scope of a using directive is limited to the file in which it appears.

There's no way do what you're trying to do; you'll have to add the alias to each file in which you wish to use it.

like image 54
Donut Avatar answered Jan 23 '26 04:01

Donut



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!