0

Is there an equivalent to this in C#?

#define TypeConstant int

int main()
{
        TypeConstant x = 5;
}

Thank you very much!

Edit: I am not sure how this is related to defining a regular constant, I have explicitly written type constant, not a constant value! Read before you vote guys!

Fatih BAKIR
  • 4,569
  • 1
  • 21
  • 27
  • C# doesn't implement a *full* precompiler, besides the `#if`, there are no structures to control the source code that is feeded to the compiler. So no macro's as well. – Willem Van Onsem Apr 12 '15 at 20:38
  • @Alejandro, although the correct answer exists in that question, I wasn't exactly looking for `typedef` but apparently, that's the way. – Fatih BAKIR Apr 12 '15 at 20:42

1 Answers1

5

It's not exactly the same, but types can be aliased by using the using Directive:

using TypeConstant = System.Int32;

As Kyle points out in the comments, you need to use the full type name here (e.g. System.Int32) instead of the C# aliases (e.g. int).

Heinzi
  • 167,459
  • 57
  • 363
  • 519
  • 1
    Yes, but that's not a precompiler directive, neither can you override it in the middle of a source file. – Willem Van Onsem Apr 12 '15 at 20:40
  • 3
    @CommuSoft: That's true, but he did not mention that any of these were a requirement. In fact, since he calls them "type constants", I seriously doubt that he *wants* to change them in the middle of a source file. – Heinzi Apr 12 '15 at 20:40
  • 2
    You might also want to explicitly mention that you can't use the C# keyword aliases for types. I.e. `using TypeConstant = int;` wouldn't work. You have to use the full type name (as you did in your answer). – Kyle Apr 12 '15 at 20:42
  • @Kyle: Good point, done! – Heinzi Apr 12 '15 at 20:44