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"); | |
| } | |
| } | |
| } |
预处理器指令
预处理器指令列表:

以#define为例:
| using System; | |
| namespace PreprocessorDAppl | |
| { | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| Console.WriteLine("PI is defined"); | |
| Console.WriteLine("PI is not defined"); | |
| Console.ReadKey(); | |
| } | |
| } | |
| } |
条件指令

| using System; | |
| public class TestClass | |
| { | |
| public static void Main() | |
| { | |
| Console.WriteLine("DEBUG is defined"); | |
| Console.WriteLine("VC_V10 is defined"); | |
| Console.WriteLine("DEBUG and VC_V10 are defined"); | |
| Console.WriteLine("DEBUG and VC_V10 are not defined"); | |
| Console.ReadKey(); | |
| } | |
| } |
正则表达式
字符转义

字符类


浙公网安备 33010602011771号