I am a little confused about the visibility of aliases with the using
directive.
I have a simple class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TestUsing
{
using StringList = List<string>;
internal class Class2
{
public void processList(StringList sl)
{ }
}
}
and I am consuming it in another file:
using TestUsing;
Class2 o = new Class2();
o.processList(new StringList());
But this doesn't compile as StringList
is not recognized.
How do I make the alias in the TestUsing
namespace visible to consumer of Class2
?
Up until C# 10, using
and using alias
directives were only local to your C# file and its scope (namespace). If you're using C# 10, you can use the global keyword and have your alias everywhere.
But if you're not, then no, aliases are not the option.
What you could do is create your own class that inherit from List
using System.Collections.Generic;
namespace TestUsing
{
public class StringList : List<string> { }
}
This can then be used everywhere you use TestUsing