Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
like image 915
Louis Rhys Avatar asked Feb 24 '12 08:02

Louis Rhys


2 Answers

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

using TimersTimer = System.Timers.Timer;

....

var myTimer = new TimersTimer();
like image 187
George Duckett Avatar answered Sep 19 '22 19:09

George Duckett


Yes.

using System.Threading;
using System.Timers;
using Timer = System.Timers.Timer;

Now Timer refers to System.Timers.Timer, but everything else from either namespace is still available without a prefix.

like image 32
Weeble Avatar answered Sep 19 '22 19:09

Weeble