Reputation: 2299
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.
Upvotes: 2
Views: 466
Reputation: 280
In C# 10 and later, you can use a global using directive: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-directive#global-modifier
Upvotes: 1
Reputation: 13060
You'll have to add the using to each file you wish to use it in...
File2:
using MyClass = System.Collections.Generic.List<int>;
namespace NamespaceC
{
class C
{
MyClass c; // Should work now.
}
}
Upvotes: 1