C#基础-12-命名空间与预处理指令

前文:

命名空间

命名空间 的设计目的是提供一种让一组名称与其他名称分隔开的方式。

在一个命名空间中声明的类的名称与另一个命名空间中声明的相同的类的名称不冲突。

例如举一个计算机系统中的例子,一个文件夹(目录)中可以包含多个文件夹,每个文件夹中不能有相同的文件名,但不同文件夹中的文件可以重名:

图自菜鸟教程。

命名空间的定义是以关键字 namespace 开始,后跟命名空间的名称,如下所示:

namespace namespace_name
{
   // 代码声明
}

调用支持命名空间版本的函数或变量,会把命名空间的名称置于前面,如下所示:

namespace_name.item_name;

实例:

using System;
namespace first_space
{
   class namespace_cl
   {
      public void func()
      {
         Console.WriteLine("Inside first_space");
      }
   }
}
namespace second_space
{
   class namespace_cl
   {
      public void func()
      {
         Console.WriteLine("Inside second_space");
      }
   }
}   
class TestClass
{
   static void Main(string[] args)
   {
      first_space.namespace_cl fc = new first_space.namespace_cl();
      second_space.namespace_cl sc = new second_space.namespace_cl();
      fc.func();
      sc.func();
      Console.ReadKey();
   }
}

输出:

Inside first_space
Inside second_space

using 关键字表明程序使用的是给定命名空间中的名称。例如在程序中使用 System 命名空间,其中定义了类 Console ,可以只写:

using System;

Console.WriteLine ("Hello there");

完全体:

System.Console.WriteLine("Hello there");

把上面的实例改为使用 using

using System;
using first_space;
using second_space;

namespace first_space
{
   class abc
   {
      public void func()
      {
         Console.WriteLine("Inside first_space");
      }
   }
}
namespace second_space
{
   class efg
   {
      public void func()
      {
         Console.WriteLine("Inside second_space");
      }
   }
}   
class TestClass
{
   static void Main(string[] args)
   {
      abc fc = new abc();
      efg sc = new efg();
      fc.func();
      sc.func();
      Console.ReadKey();
   }
}

命名空间可以被嵌套,即可以在一个命名空间内定义另一个命名空间,如下所示:

namespace namespace_name1 
{
   // 代码声明
   namespace namespace_name2 
   {
     // 代码声明
   }
}

可以使用点( . )运算符访问嵌套的命名空间的成员,如下所示:

using System;
using SomeNameSpace;
using SomeNameSpace.Nested;

namespace SomeNameSpace
{
    public class MyClass 
    {
        static void Main() 
        {
            Console.WriteLine("In SomeNameSpace");
            Nested.NestedNameSpaceClass.SayHello();
        }
    }

    // 内嵌命名空间
    namespace Nested   
    {
        public class NestedNameSpaceClass 
        {
            public static void SayHello() 
            {
                Console.WriteLine("In Nested");
            }
        }
    }
}

输出:

In SomeNameSpace
In Nested

预处理指令

预处理器指令(Preprocessor Directives)指导编译器在实际 编译开始之前 对信息进行预处理。

通过这些指令,可以控制编译器如何编译文件或编译哪些部分。常见的预处理器指令包括条件编译、宏定义等。

所有的预处理器指令都是以 # 开始,且在 一行 上,只有 空白字符 可以出现在预处理器指令之前。

预处理器指令不是语句,所以它们不以分号 ; 结束。

C# 编译器没有一个单独的预处理器,但是,指令被处理时就像是有一个单独的预处理器一样。在 C# 中,预处理器指令用于在条件编译中起作用。与 C 和 C++ 不同的是,它们不是用来创建宏。一个预处理器指令必须是该行上的唯一指令。

下表列出了 C# 中可用的预处理器指令:

指令 描述
#define 定义一个符号,可以用于条件编译。
#undef 取消定义一个符号。
#if 开始一个条件编译块,如果符号被定义则包含代码块。
#elif 如果前面的 #if#elif 条件不满足,且当前条件满足,则包含代码块。
#else 如果前面的 #if#elif 条件不满足,则包含代码块。
#endif 结束一个条件编译块。
#warning 生成编译器警告信息。
#error 生成编译器错误信息。
#region 标记一段代码区域,可以在IDE中折叠和展开这段代码,便于代码的组织和阅读。
#endregion 结束一个代码区域。
#line 更改编译器输出中的行号和文件名,可以用于调试或生成工具的代码。
#pragma 用于给编译器发送特殊指令,例如禁用或恢复特定的警告。
#nullable 控制可空性上下文和注释,允许启用或禁用对可空引用类型的编译器检查。

例如:

#define DEBUG

#if DEBUG
    Console.WriteLine("Debug mode");
#elif RELEASE
    Console.WriteLine("Release mode");
#else
    Console.WriteLine("Other mode");
#endif

#warning This is a warning message
#error This is an error message

#region MyRegion
    // Your code here
#endregion

#line 100 "MyFile.cs"
    // The next line will be reported as line 100 in MyFile.cs
    Console.WriteLine("This is line 100");
#line default
    // Line numbering returns to normal

#pragma warning disable 414
    private int unusedVariable;
#pragma warning restore 414

#nullable enable
    string? nullableString = null;
#nullable disable

define和undef

#define 用于定义符号(通常用于条件编译),#undef 用于取消定义符号。

#define DEBUG

#undef RELEASE

#define 允许定义一个符号,这样,通过使用符号作为传递给 #if 指令的表达式,表达式将返回 true ,示例如下:

#define PI 
using System;
namespace PreprocessorDAppl
{
   class Program
   {
      static void Main(string[] args)
      {
         #if (PI)
            Console.WriteLine("PI is defined");
         #else
            Console.WriteLine("PI is not defined");
         #endif
         Console.ReadKey();
      }
   }
}

输出:

PI is defined

条件指令

条件指令:#if , #elif , #else#endif

条件指令用于测试符号是否为真。如果为真,编译器会执行 #if 和下一个指令之间的代码。

示例:

#define DEBUG
#define VC_V10 // 编译器版本
using System;
public class TestClass
{
   public static void Main()
   {
      #if (DEBUG && !VC_V10)
         Console.WriteLine("DEBUG is defined");
      #elif (!DEBUG && VC_V10)
         Console.WriteLine("VC_V10 is defined");
      #elif (DEBUG && VC_V10)
         Console.WriteLine("DEBUG and VC_V10 are defined");
      #else
         Console.WriteLine("DEBUG and VC_V10 are not defined");
      #endif
      Console.ReadKey();
   }
}

输出:

DEBUG and VC_V10 are defined

其他指令

#warning 用于生成编译器警告,#error 用于生成编译器错误:

#warning This is a warning message
#error This is an error message

#region#endregion 用于代码折叠,使代码更加可读:

#region MyRegion
    // Your code here
#endregion

#line 用于更改文件行号和文件名的编译器输出:

#line 100 "MyFile.cs"
    // The next line will be reported as line 100 in MyFile.cs
    Console.WriteLine("This is line 100");
#line default
    // Line numbering returns to normal

#pragma 用于向编译器发送特殊指令。最常见的用法是禁用特定的警告:

#pragma warning disable 414
    private int unusedVariable;
#pragma warning restore 414

注意事项

  • 提高代码可读性:使用 #region 可以帮助分隔代码块,提高代码的组织性。
  • 条件编译:通过 #if 等指令可以在开发和生产环境中编译不同的代码,方便调试和发布。
  • 警告和错误:通过 #warning#error 可以在编译时提示开发人员注意特定问题。

通过正确使用这些预处理器指令,可以更好地控制代码的编译过程,提高代码的灵活性和可维护性。