Can the C# compiler insert constant values that auto increment on each usage at compile time?

Is there a way to have the C# compiler insert constant values that auto-increment at compile time?

E.g.

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

Would produce this code:

   MyFunc(1);
   MyFunc(2);
   MyFunc(3);
Jon Skeet
people
quotationmark

No, there's nothing in the language that does this.

There are some grotty hacks that would allow you to keep track of the caller file/line/member, and auto-increment based on that (if you're using C# 5) - but it wouldn't really be the same.

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

people

See more on this question at Stackoverflow