Search code examples
c#.netnamespacescollision

Is it possible to disambiguate conflicting type name in the using declaration?


For example, both System.Threading and System.Timers has the class Timer. So if I would like to use System.Timers.Timer in a class that uses System.Threading, I have to use stuff like

System.Timers.Timer myTimer = new System.Timers.Timer();

everytime I want to declare/initialize timers. Is there a way to tell the compiler that "whenever I say Timer, use System.Timers.Timer unless declared otherwise?" So that I can use simple

Timer t = new Timer();

in default cases to mean System.Timers.Timer, unless I specify explicitly

System.Threading.Timer t2 = new System.Threading.Timer();

Solution

  • You can create an alias for a type (or namespace). See using Directive (MSDN)

    using TimersTimer = System.Timers.Timer;
    
    ....
    
    var myTimer = new TimersTimer();