Can the C # compiler insert constant values โ€‹โ€‹that are automatically incremented with each use at compile time?

Is there a way for the C # compiler to insert constant values โ€‹โ€‹that are automatically incremented at compile time?

eg.

MyFunc(NEXT_CONSTANT);
MyFunc(NEXT_CONSTANT);
MyFunc(NEXT_CONSTANT);

      

Would create this code:

   MyFunc(1);
   MyFunc(2);
   MyFunc(3);

      

+3


source to share


2 answers


No, there is nothing in this language.

There are some cumbersome hacks that will let you keep track of the caller's file / line / member and automatically increment it (if you're using C # 5), but it really won't.



There are tool-based approaches to this that will transform your source code, but I'll try to take a step back and look at your actual requirements and intentions (which we don't know at the moment) and try to find a solution within the language if you can.

+4


source


One such thing that does this is the build number of the build version. If you install build version eg. 1.2.*

, the last two digits will be changed with every build. You can easily read this in your code - it is not a constant, but if you expose it via a static property it might work fine.



If that's not enough, just create a build target. It shouldn't be difficult to maintain a nearly empty C # file with a fixed structure that you can modify prior to each build.

0


source







All Articles