• Welcome to Dark Bit Factory & Gravity.
 

[c#] Is there something like macros?

Started by va!n, February 23, 2011

Previous topic - Next topic

0 Members and 0 Guests are viewing this topic.

va!n

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#?


  #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:

Jim

No, C# doesn't have macros.  You could use a static class.

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:

va!n

#2
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++
- 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:

Jim

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: