Reputation: 35637
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();
Upvotes: 8
Views: 3768
Reputation: 17950
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.
Upvotes: 6
Reputation: 2180
using MyAlias = System.Timers.Timer;
var foo = new MyAlias();
Upvotes: 1
Reputation: 32448
You can create an alias for a type (or namespace). See using Directive (MSDN)
using TimersTimer = System.Timers.Timer;
....
var myTimer = new TimersTimer();
Upvotes: 10