首页 > 技术文章 > c# using 引用和别名的使用

crz2014 2015-03-08 17:24 原文

1.使用别名

在同时引用的两个命名空间中有相同的类型时,可以使用别名来区分。如下所示:

using System;

using System.Threading;

using System.Timers;

其中在第二个和第三个引入的命名空间中有相同的Timer名字,这样可以使用using CountDownTimer=System.Timers.Timer;来为其中一个起一个别名来避免重名。也可以将别名设定为Timer。

using Timer=System.Timers.Timer;

class HelloWorld

{static void Main()

          { Timer timer;}}

现在使用Timer便没有歧义,如果再引用System.Threading.Timer类需要完全限定或者定义不同的别名。

推荐阅读