C# 奇数命名空间声明

C# 奇数命名空间声明,c#,namespaces,C#,Namespaces,因此,我在工作中浏览了一些较旧的代码,发现了以下内容: using Int16 = System.Int16; using SqlCommand = System.Data.SqlClient.SqlCommand; 我以前从未见过名称空间声明使用“=”。使用它有什么意义?这样申报有什么好处吗 让我感到奇怪的是,他们甚至懒得申报Int16。VisualStudio难道不通过键入Int16就知道它是什么吗?第一行是。。。嗯。。。没有什么意义,但它不是名称空间导入;这是一个好主意。例如,int是I

因此,我在工作中浏览了一些较旧的代码,发现了以下内容:

using Int16 = System.Int16;
using SqlCommand = System.Data.SqlClient.SqlCommand;
我以前从未见过名称空间声明使用“=”。使用它有什么意义?这样申报有什么好处吗


让我感到奇怪的是,他们甚至懒得申报Int16。VisualStudio难道不通过键入Int16就知道它是什么吗?

第一行是。。。嗯。。。没有什么意义,但它不是名称空间导入;这是一个好主意。例如,
int
Int32
的别名。正如您在示例中所示,您完全可以自由创建自己的别名

例如,假设您必须导入具有两种同名类型的名称空间(
System.Drawing.Point
System.Windows.Point
)。您可以创建别名,以避免在代码中完全限定这两种类型

using WinFormsPoint = System.Drawing.Point;
using WpfPoint = System.Windows.Point;

void ILikeMyPointsStructy( WinFormsPoint p ) { /* ... */ }
void IPreferReferenceTypesThankYou( WpfPoint p ) { /* ... */ }

第一行是。。。嗯。。。没有什么意义,但它不是名称空间导入;这是一个好主意。例如,
int
Int32
的别名。正如您在示例中所示,您完全可以自由创建自己的别名

例如,假设您必须导入具有两种同名类型的名称空间(
System.Drawing.Point
System.Windows.Point
)。您可以创建别名,以避免在代码中完全限定这两种类型

using WinFormsPoint = System.Drawing.Point;
using WpfPoint = System.Windows.Point;

void ILikeMyPointsStructy( WinFormsPoint p ) { /* ... */ }
void IPreferReferenceTypesThankYou( WpfPoint p ) { /* ... */ }

名称空间别名有助于简化访问某些类型的方式,特别是当您有许多名称冲突的类型时

例如,如果您正在引用两个不同的名称空间,其中定义了不同的常量集,如:

namespace Library
{
   public static class Constants
   {
      public const string FIRST = "first";
      public const string SECOND = "second";
   }
}

namespace Services
{
   public static class Constants
   {
      public const string THIRD = "third";
      public const string FOURTH = "fourth";
   }
}
然后,您决定在代码文件中同时使用这两种方法--只需编写以下代码,就会出现编译错误:

var foo = Constants.FIRST;
另一种方法是完全限定您的常量,这可能是一个难题,因此名称空间别名简化了它:

using Constants = Library.Constants;
using ServiceConstants = Service.Constants;

话虽如此,我不知道你为什么把Int16别名为Int16

名称空间别名有助于简化访问某些类型的方式,特别是当您有许多名称冲突的类型时

例如,如果您正在引用两个不同的名称空间,其中定义了不同的常量集,如:

namespace Library
{
   public static class Constants
   {
      public const string FIRST = "first";
      public const string SECOND = "second";
   }
}

namespace Services
{
   public static class Constants
   {
      public const string THIRD = "third";
      public const string FOURTH = "fourth";
   }
}
然后,您决定在代码文件中同时使用这两种方法--只需编写以下代码,就会出现编译错误:

var foo = Constants.FIRST;
另一种方法是完全限定您的常量,这可能是一个难题,因此名称空间别名简化了它:

using Constants = Library.Constants;
using ServiceConstants = Service.Constants;

话虽如此,我不知道你为什么把Int16别名为Int16

> P>对于来自C++背景的开发人员,该构造也可以被用作一种“局部Type”,这有助于简化通用容器定义:-< /P>
using Index = Dictionary<string, MyType>;

private Index BuildIndex(. . .)
{
  var index = new Index();
  . . .
  return index;
}
使用索引=字典;
私有索引BuildIndex(…)
{
var指数=新指数();
. . .
收益指数;
}

< /代码>

对于来自C++背景的开发人员,该构造也可以被用作一种“局部Type”,这有助于简化通用容器定义:-< /P>

using Index = Dictionary<string, MyType>;

private Index BuildIndex(. . .)
{
  var index = new Index();
  . . .
  return index;
}
使用索引=字典;
私有索引BuildIndex(…)
{
var指数=新指数();
. . .
收益指数;
}

Int16位也让我感到困惑。在那个代码文件中有很多类似的东西!Int16位也让我困惑。在那个代码文件中有很多类似的东西!