Author Topic: [c#] Is there something like macros?  (Read 3962 times)

0 Members and 1 Guest are viewing this topic.

Offline va!n

  • Pentium
  • *****
  • Posts: 1435
  • Karma: 109
    • View Profile
    • http://www.secretly.de
[c#] Is there something like macros?
« on: February 23, 2011 »
I would like to use in C# something like macros known in C/C++... Sadly it seems C# does not supports macros... However i have a lot of macros in mind and i dont want to write the same codeblock (macro) again and again inside my project and i dont want to use public methode, because this would be slow down the code, even when its called x times inside a loop.

So any idea, how to use something like macros in C#?

Code: [Select]
  #define MULT(x, y) x * y    // just as simple example
- hp EliteBook 8540p, 4 GB RAM, Windows 8.1 x64
- Asus P5Q, Intel Q8200, 6 GB DDR2, Radeon 4870, Windows 8.1 x64
http://www.secretly.de
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: [c#] Is there something like macros?
« Reply #1 on: February 24, 2011 »
No, C# doesn't have macros.  You could use a static class.
Code: [Select]
public static class Macros
{
  public static int Mult(int a, int b) { return a*b; }
}

...

int myanswer = Macros.Mult(5,4);

Let the compiler work out if it can inline that.

Jim
Challenge Trophies Won:

Offline va!n

  • Pentium
  • *****
  • Posts: 1435
  • Karma: 109
    • View Profile
    • http://www.secretly.de
Re: [c#] Is there something like macros?
« Reply #2 on: February 24, 2011 »
Thanks for the fast reply Jim! So, when using "static", the compiler could probaly inline it?  :kewl:

[Edited - Added:]
Thanks a lot for this great tip! It works great! K++
« Last Edit: February 24, 2011 by va!n »
- hp EliteBook 8540p, 4 GB RAM, Windows 8.1 x64
- Asus P5Q, Intel Q8200, 6 GB DDR2, Radeon 4870, Windows 8.1 x64
http://www.secretly.de
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: [c#] Is there something like macros?
« Reply #3 on: February 24, 2011 »
I have no idea.  The IL is dynamically recompiled and optimised at run-time and it's possible.
But it's nothing like C where you can determine this at compile time.

Jim
Challenge Trophies Won: