2024-11-18《命名与预处理指令学习》

命名空间

  命名空间在基础的C#代码里就有使用,使用关键字namespace,我们可以通过调用其后面的命名空间里的类来进行操作。

using关键字

  通过using关键字,我们可以将上述的namespace来进行简化,可以直接使用里面的类来进行操作。

嵌套命名空间

  我们可以使用嵌套命名空间,可以进行复用:

  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");
  }
  }
  }

预处理器指令

  预处理器指令列表:

image-20221014161234952

  以#define为例:

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

条件指令

image-20221014161428509

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

正则表达式

字符转义

image-20221014161607030

字符类

image-20221014161642246

image-20221014161701775

posted @ 2024-12-29 14:37  new菜鸟  阅读(11)  评论(0)    收藏  举报