Louis Rhys
Louis Rhys

Reputation: 35637

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();

Upvotes: 8

Views: 3768

Answers (3)

Weeble
Weeble

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

jstephenson
jstephenson

Reputation: 2180

See The using Directive

using MyAlias = System.Timers.Timer;
var foo = new MyAlias();

Upvotes: 1

George Duckett
George Duckett

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

Related Questions