C# Macro definitions in Preprocessor

前端 未结 10 1690
走了就别回头了
走了就别回头了 2020-11-29 04:16

Is C# able to define macros as is done in the C programming language with pre-processor statements? I would like to simplify regular typing of certain repeating statements

相关标签:
10条回答
  • 2020-11-29 04:56

    While you can't write macros, when it comes to simplifying things like your example, C# 6.0 now offers static usings. Here's the example Martin Pernica gave on his Medium article:

    using static System.Console; // Note the static keyword
    
    namespace CoolCSharp6Features
    {
      public class Program
      {
        public static int Main(string[] args)
        {
          WriteLine("Hellow World without Console class name prefix!");
    
          return 0;
        }
      }
    }
    
    0 讨论(0)
  • 2020-11-29 05:04

    Luckily, C# has no C/C++-style preprocessor - only conditional compilation and pragmas (and possibly something else I cannot recall) are supported. Unfortunatelly, C# has no metaprogramming capabilities (this may actually relate to your question to some extent).

    0 讨论(0)
  • 2020-11-29 05:04

    Since C# 7.0 supports using static directive and Local functions you don't need preprocessor macros for most cases.

    0 讨论(0)
  • 2020-11-29 05:06

    Turn the C Macro into a C# static method in a class.

    0 讨论(0)
提交回复
热议问题