惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Stack Overflow Blog
Stack Overflow Blog
T
Tailwind CSS Blog
Recent Announcements
Recent Announcements
宝玉的分享
宝玉的分享
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
P
Proofpoint News Feed
D
Docker
Google DeepMind News
Google DeepMind News
aimingoo的专栏
aimingoo的专栏
B
Blog RSS Feed
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
M
MIT News - Artificial intelligence
云风的 BLOG
云风的 BLOG
小众软件
小众软件
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
IT之家
IT之家
H
Help Net Security
Apple Machine Learning Research
Apple Machine Learning Research
Martin Fowler
Martin Fowler
S
SegmentFault 最新的问题
B
Blog
D
DataBreaches.Net

博客园 - 真见

用户中心 - 博客园 如何在ASP.NET中使用Windows Live Web Bar 博客园的一篇好文 Internet Explorer 8.0 正式版 提交按钮OnCheck之后的技巧 - 真见 - 博客园 如何在ASP.NET中使用Syndication创建一个RSS源 如何创建Sql Server数据库关系图 + Silverlight 2 GDR1 发布 ASP.NET WebForms 4.0 新属性 如何在ASP.NET中使用验证通过的Windows Live ID用户登录网站 我的Windows 7系统 Internet Explorer 8 RC1发布 新浪首届我的创业路征文大赛初评榜单公布 得到微软DevWow博客达人征文大赛优秀奖,奖得微软T-Shirt一件 我的五项 Windows Live Writer 使用习惯分享 + 来自 Jackson Fish Market 的虚拟鲜花赠送服务推荐 摇滚真见的生日 Sueetie源代码发布【 推荐 】 中国版 Windows Live Wave3 介绍站点 ASP.NET网站编程人员2008年技术文章参照 Visual Studio可视化IDE风格主题参照
Using-更精彩更有用的做法-短签名
真见 · 2009-02-03 · via 博客园 - 真见

一般我们编程的时候Using默认一般做法如下:

 using System;  
 using System.Collections.Generic;  
 using System.Text.RegularExpressions;  
 using System.Net;  
 using System.IO; 

但是,有很多情况,比如,现在有2个类,类名都叫Class1,2个类在不同的名称空间,现在要在同一文件中提取这2个类,你一般做法是:

namespace  SampleClass{
     class TestClass{
         private void F(){
              Namespace.one.Class1 c1= new Namespace.one.Class1();
              Namespace.two.Class1 c2= new Namespace.two.Class1();
         }
    }
}
或者
using Namespace.one;

namespace  SampleClass{
     class TestClass{
         private void F(){
              Class1 c1= new Class1();
              Namespace.two.Class1 c2= new Namespace.two.Class1();
         }
    }
}
 

现在,有一种解决方法避免这种情况,当然也不算避免,但是可以使用更短的名字:有2中方法,第一种叫别名称,

using Namespace.one;
using T = Namespace.two;

namespace  SampleClass{
     class TestClass{
         private void F(){
              Class1 c1= new Class1();
              T.Class1 c2= new T.Class1();
         }
    }
}

第二种方法叫 别类名:

using Namespace.one;
using T = Namespace.two.Class1;

namespace  SampleClass{
     class TestClass{
         private void F(){
              Class1 c1= new Class1();
              T c2= new T();
         }
    }
}

怎么样,Class1和T那个更短,这让我想起来了现在流行的短URL服务网站兴起的重要。。